#Activate body
1 messages · Page 1 of 1 (latest)
here is a thread for your issue
post everything here so it's easy to look over the different aspects of your project
ok....
can't keep scrolling forever when helping multiple people :<
basically that thing doesn t work and it s called from another script
if(CollisionInfo.collider.tag == "Body")
{
FindObjectOfType<body_parts>();
}
thats kinda it
FindObjectOfType cannot find an inactive game object
but it s not searching for an inactive game object
ok
it s searching for a script that isn t on that game object
where is the script then?
on the character game object
this is the whole inspector
you can see in the script that i reference the game object
if you have indeed referenced it in the inspector of the script
that body object doesn't have the script attached to it?
i don t need the body object to have it
the character object has it
I'm not sure I'm following
wait guys when using the FindObjectOfType<body_parts>(); does it check the whole script or?
the whole script?
yeah cuz when i tried FindObjectOfType<body_parts>().update(); it didn t work
gave an error
FindObjectOfType<body_parts>();
will return the first object which has a body_parts script as a component
what was the error?
wut?
you don't call update no, just call your own public function
this
Assets\scripts\PlayerCollision.cs(20,44): error CS0122: 'body_parts.Update()' is inaccessible due to its protection level
why are you trying to access the Update() function of another script? that is very bad practice
ok but i think i need this to run everytime don t i? like if i unactivate the body again and colide with it again to activate it....
i have no idea what you are trying to do but no, you can toggle it only when the collision happens
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
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
not sure where to find a tutorial for this
wait a seccond
Unity Learn tutorials are also useful
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
indeed, why not just
[SerializeField] private GameObject someBody;
a bit hard to explain i guess
parenting and unparenting
have no clue what is [SerializeField] :/
it exposes the variable to the inspector, so you can drag-drop objects into it
same as public
but not accessible to other scripts
and how do i do that in runtime?like when the game is running
you don't - you do this in the Editor
this is a main game mechanic i want....
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
what i want to achive here is a system where to player can detach and atach body parts
yes
ok and how does the attach and detach process happen?
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.
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
then you can
but it gotta atach the right way not like having your legs on top of your head
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("body"))
{
// Attach
}
}
isn t that the same thing but with compare tag?
are you wanting to parent the same object you collide with or activate/create a new object?
I'm a bit tired, so I hope smallg can help find a solution
as you can see in the inspect i got the same object but unnactivated so when it collides i want to activate it
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? 🤔
not sure if its the best method to do it but yeah
ok so you are assigning the same script to each part to use it as a form of identifier?
the goal is that when the player head collides with a body part in the game, it will apear like he has that body part attached, the problem is that this doesn t work rn
i reference each part in that script
as i referenced the body
ok so why can't you activate the reference?
im sorry if this doesn t make any sense im just a (very) begginer trying to do something....
wait a minute
the problem right now is that when i collide with the body part nothing happens
and i don t know why
So when the head collided with an arm, that arm will be attached to the head?
@mint jetty
are you trying to attach the object you're colliding with, or a mirror copy of it already attached to the player?
This was my next question . . . 😂
maybe.... if not im gonna need a system to check what body parts it has but for now that s the main idea
sounds like he is trying to activate a copy of it that is already attached to the player
"Maybe" doesn't help. If you're not sure how it's going to work, we can't give a valid answer. If that's not what you're trying to do, then what is it you are trying to do?
well if you know where each part should go you can parent and position the existing object easily enough
it's not necessary to do it that way, but you CAN do it that way if you serialize the reference (inspector drag drop)
i fucking did that with the body part
but it kinda comes to the same in the end, you still need to know where it needs to go
i need to know if it has the body it will be able to have the legs if not it won t
that s the logic i go with here.....
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
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 . . .
this tryes to find a script witch isn t attached to the disabled game object FindObjectOfType<body_parts>().BodyParts();
that s the script name
Find methods don't work on deactivated objects . . .
but it s not searching for a deactivated object
can you post your scripts for the attaching process?
ok wait a seccond
these are the collision and the body parts script
man we are here for over 40 minutes.....
So you find the body part class and call BodyParts. What does that do?
so the one you are colliding with has a reference in the "body" to the part on the player?
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 . . .
so it looks like you want to find the script attached to the collision rather than findall
findall?
No, that just checks if the collided object has that tag. If it does, you use find which searches and returns the first active object with that class . . .
oh
You need to reference the body part of the collided object and call the method on its class . . .
that GameObject body is the referenced colided body part in a way....
in the body parts script
right?
If you need to check a component, you can skip the tag check, as you would be doing two checks instead of one . . .
yes, use CollisionInfo.transform.GetComponent<body_parts>().BodyParts();
That's in the body part script, not in the collision script . . .
well yeah
But you need the reference in the collision script . . .
sorry im kinda confused here.....
ah so make a similar script for the collided part
why would i do that?
Just use GetComponent on the collided object, that's all you nerd to do . . .
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
You replace the FindObject line with the GetComponent line . . .
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
Ok, what line is that?
did you pass in the value in the inspector?
it s that line from the if statement
Ok, which one exactly?
this one
CollisionInfo.transform.GetComponent<body_parts>().BodyParts();
replied to the wront comment
.
So the object doesn't have a body part class on it . . .
ok you need to check body_parts is not null first
Did you remove the if statement?
Try CollisionInfo.collider instead . . .
is there a body_parts script on the object you are colliding with or not?
im already using that if(CollisionInfo.collider.CompareTag("Body"))
right?
no
CollisionInfo.transform
CollisionInfo.collider.transform ?
there isn t
that's why then
If there is no body part, then how can you access the body part script?
you need a script like that that you can set on the collision part so it knows where to go
That's the whole point of the tag and component check . . .
there is that game object called character right there....
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
Scripts need to be placed on the correct objects that correspond to what they do . . .
you want body_parts script to be on the object you are colliding with
thats why i used the damn reference in the script
huh? why
then reference "body" to the part it should activate
because you need a different "body" for each part depending on which object you hit
NullReferenceException: Object reference not set to an instance of an object
PlayerCollision.OnCollisionEnter2D (UnityEngine.Collision2D CollisionInfo) (at Assets/scripts/PlayerCollision.cs:20)
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?
yep
the same one right?
drag the one that is attached to the player
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
yes, you can just call Destroy(CollisionInfo.gameObject)
yeah
after you reference the script
so basically we stayd here an hour just for 1 little thing :/
yes, just need to put the script on the correct object 🙂
well thanks for the help everyone
no worries, good luck 🙂