#Activate body

1 messages · Page 1 of 1 (latest)

hollow quest
#

here is a thread for your issue

#

post everything here so it's easy to look over the different aspects of your project

mint jetty
#

ok....

hollow quest
#

can't keep scrolling forever when helping multiple people :<

mint jetty
#

basically that thing doesn t work and it s called from another script

#

if(CollisionInfo.collider.tag == "Body")
{

     FindObjectOfType<body_parts>();
    }
#

thats kinda it

hollow quest
#

FindObjectOfType cannot find an inactive game object

mint jetty
#

but it s not searching for an inactive game object

hollow quest
#

ok

mint jetty
#

it s searching for a script that isn t on that game object

hollow quest
#

where is the script then?

mint jetty
#

on the character game object

#

this is the whole inspector

#

you can see in the script that i reference the game object

hollow quest
#

if you have indeed referenced it in the inspector of the script

crystal anchor
#

that body object doesn't have the script attached to it?

mint jetty
#

the character object has it

hollow quest
#

I'm not sure I'm following

mint jetty
#

wait guys when using the FindObjectOfType<body_parts>(); does it check the whole script or?

crystal anchor
#

the whole script?

mint jetty
#

yeah cuz when i tried FindObjectOfType<body_parts>().update(); it didn t work

#

gave an error

hollow quest
#

FindObjectOfType<body_parts>();
will return the first object which has a body_parts script as a component

#

what was the error?

mint jetty
#

wut?

crystal anchor
#

you don't call update no, just call your own public function

mint jetty
#

Assets\scripts\PlayerCollision.cs(20,44): error CS0122: 'body_parts.Update()' is inaccessible due to its protection level

hollow quest
#

why are you trying to access the Update() function of another script? that is very bad practice

mint jetty
crystal anchor
#

i have no idea what you are trying to do but no, you can toggle it only when the collision happens

hollow quest
#

Have you looked at tutorials relevant to what you are trying to achieve?

#

I am certain there is a better approach

#

you want to avoid having scripts interacting with one another too much, as it will make for a lot of pain when managing Script Execution Order

crystal anchor
#

btw if the objects are always in the scene it's far better to just pass in a reference rather than looking for them in runtime

mint jetty
hollow quest
#

youtube

#

what is it you are trying to achieve, in simple terms?

mint jetty
#

wait a seccond

hollow quest
#

Unity Learn tutorials are also useful

mint jetty
#

so basically im trying to do a system where if the player head collides with a body part he will have that body part united

hollow quest
mint jetty
#

a bit hard to explain i guess

mint jetty
hollow quest
#

it exposes the variable to the inspector, so you can drag-drop objects into it

#

same as public

#

but not accessible to other scripts

mint jetty
#

and how do i do that in runtime?like when the game is running

hollow quest
#

you don't - you do this in the Editor

mint jetty
#

this is a main game mechanic i want....

hollow quest
#

unless you're creating new body-parts during run-time

#

if they can all exist while you are editing your game, then just drag-drop them in the inspector

mint jetty
#

what i want to achive here is a system where to player can detach and atach body parts

hollow quest
#

yes

crystal anchor
#

ok and how does the attach and detach process happen?

hollow quest
#

so you create game objects for arms, legs, head, body
and in the script you make a SerializeField variable for each of them
Drag-drop the objects from the hierarchy to the inspector field
and voila - you have your references.

mint jetty
#

thought that when the player colides with a body part the body part attach to it, so i just activate it in the character game object

hollow quest
#

then you can

mint jetty
#

but it gotta atach the right way not like having your legs on top of your head

hollow quest
#
private void OnCollisionEnter(Collision collision)
{
    if (collision.gameObject.CompareTag("body"))
    {
        // Attach
    }
}
mint jetty
#

isn t that the same thing but with compare tag?

hollow quest
#

hm, yeah lol

#

but comparetag is recommended

#

try it

crystal anchor
#

are you wanting to parent the same object you collide with or activate/create a new object?

hollow quest
#

I'm a bit tired, so I hope smallg can help find a solution

mint jetty
pastel merlin
#

What are you actually trying to do? Attempted to follow the convo but nothing makes sense as to what your goal is or the exact problem you have? 🤔

mint jetty
#

not sure if its the best method to do it but yeah

crystal anchor
#

ok so you are assigning the same script to each part to use it as a form of identifier?

mint jetty
mint jetty
#

as i referenced the body

crystal anchor
#

ok so why can't you activate the reference?

mint jetty
#

im sorry if this doesn t make any sense im just a (very) begginer trying to do something....

hollow quest
#

wait a minute

mint jetty
#

and i don t know why

pastel merlin
hollow quest
#

@mint jetty
are you trying to attach the object you're colliding with, or a mirror copy of it already attached to the player?

pastel merlin
mint jetty
crystal anchor
#

sounds like he is trying to activate a copy of it that is already attached to the player

mint jetty
#

yeah thought it would be the easiest way to do it

#

isn t it?

pastel merlin
crystal anchor
#

well if you know where each part should go you can parent and position the existing object easily enough

hollow quest
#

it's not necessary to do it that way, but you CAN do it that way if you serialize the reference (inspector drag drop)

mint jetty
#

i fucking did that with the body part

crystal anchor
#

but it kinda comes to the same in the end, you still need to know where it needs to go

mint jetty
#

that s the logic i go with here.....

hollow quest
#

for the sake of keeping you guys up to date - one problem is that he tried to use FindObject to try to find a disabled game object

pastel merlin
#

If you collide with an arm and you want to activate the arm attached to the player. That's fine, and should be simple. Get the type of body part collided with, and then search the player components (scripts) for that type and activate its object (or enable its script) . . .

#

That's all you need to do. Prolly best to have a reference of each body part in a list so you can search through the list to access each one . . .

mint jetty
#

that s the script name

pastel merlin
#

Find methods don't work on deactivated objects . . .

mint jetty
crystal anchor
#

can you post your scripts for the attaching process?

mint jetty
#

ok wait a seccond

#

these are the collision and the body parts script

#

man we are here for over 40 minutes.....

pastel merlin
#

So you find the body part class and call BodyParts. What does that do?

crystal anchor
#

so the one you are colliding with has a reference in the "body" to the part on the player?

pastel merlin
#

Problem is, you're not referencing the collided body part . . .

#

You're just getting the first active body part script and calling it on that object . . .

mint jetty
#

im referencing the tag for it

#

look in the collision script

crystal anchor
#

so it looks like you want to find the script attached to the collision rather than findall

mint jetty
#

findall?

pastel merlin
mint jetty
#

oh

pastel merlin
#

You need to reference the body part of the collided object and call the method on its class . . .

mint jetty
#

that GameObject body is the referenced colided body part in a way....

#

in the body parts script

#

right?

pastel merlin
#

If you need to check a component, you can skip the tag check, as you would be doing two checks instead of one . . .

crystal anchor
#

yes, use CollisionInfo.transform.GetComponent<body_parts>().BodyParts();

pastel merlin
mint jetty
#

well yeah

pastel merlin
#

But you need the reference in the collision script . . .

mint jetty
#

sorry im kinda confused here.....

crystal anchor
#

ah so make a similar script for the collided part

mint jetty
#

why would i do that?

pastel merlin
#

Just use GetComponent on the collided object, that's all you nerd to do . . .

mint jetty
#

ok

#

do i put it in the if statement?

#

like the thing rn is it doesn t even give any errors it just doesn t work

pastel merlin
#

You replace the FindObject line with the GetComponent line . . .

mint jetty
#

ok

#

now i have another problem :/

#

NullReferenceException: Object reference not set to an instance of an object
PlayerCollision.OnCollisionEnter2D (UnityEngine.Collision2D CollisionInfo) (at Assets/scripts/PlayerCollision.cs:20)

#

think it s because the body object is unactive

pastel merlin
#

Ok, what line is that?

crystal anchor
#

did you pass in the value in the inspector?

mint jetty
#

it s that line from the if statement

pastel merlin
#

Ok, which one exactly?

mint jetty
#

CollisionInfo.transform.GetComponent<body_parts>().BodyParts();

#

replied to the wront comment

mint jetty
pastel merlin
#

So the object doesn't have a body part class on it . . .

crystal anchor
#

ok you need to check body_parts is not null first

pastel merlin
#

Did you remove the if statement?

mint jetty
#

no

#

this is how it is

pastel merlin
#

Try CollisionInfo.collider instead . . .

crystal anchor
#

is there a body_parts script on the object you are colliding with or not?

mint jetty
#

right?

hollow quest
#

CollisionInfo.transform
CollisionInfo.collider.transform ?

mint jetty
#

there isn t

crystal anchor
#

that's why then

pastel merlin
#

If there is no body part, then how can you access the body part script?

crystal anchor
#

you need a script like that that you can set on the collision part so it knows where to go

pastel merlin
#

That's the whole point of the tag and component check . . .

mint jetty
#

and im having that script there

#

but the collision script is on the player head

#

im getting a bit lost....

#

the problem rn is that nothing happens when colliding

#

and i have no clue why

pastel merlin
#

Scripts need to be placed on the correct objects that correspond to what they do . . .

crystal anchor
#

you want body_parts script to be on the object you are colliding with

mint jetty
#

thats why i used the damn reference in the script

crystal anchor
#

then reference "body" to the part it should activate

mint jetty
#

ok wait a sec im gonna try that

#

but i still have that error

crystal anchor
#

because you need a different "body" for each part depending on which object you hit

mint jetty
#

NullReferenceException: Object reference not set to an instance of an object
PlayerCollision.OnCollisionEnter2D (UnityEngine.Collision2D CollisionInfo) (at Assets/scripts/PlayerCollision.cs:20)

crystal anchor
#

yes

#

because the collided object doesn't have the script

mint jetty
#

ok ok so after putting the script on the game object i want to collide with

#

i put the part i want to activate in the body reference?

crystal anchor
#

yep

mint jetty
#

the same one right?

crystal anchor
#

drag the one that is attached to the player

mint jetty
#

HOW THE FUCK DOES THAT WORK

#

it activates :D

#

thanks

#

now i need to delete that object after colliding and sincronize the body part position with the player position

crystal anchor
#

yes, you can just call Destroy(CollisionInfo.gameObject)

mint jetty
#

yeah

crystal anchor
#

after you reference the script

mint jetty
#

so basically we stayd here an hour just for 1 little thing :/

crystal anchor
#

yes, just need to put the script on the correct object 🙂

mint jetty
#

well thanks for the help everyone

crystal anchor
#

no worries, good luck 🙂

hollow quest
#

Just please post the solution if you find one :D

#

no matter how silly it might be

#

oh wait

#

you already solved it

#

I need a break lol

#

good stuff