- Home /
Switch for children not working
Hi there, I have a parent object with 120 childs, the code is on the parent object and I need to make a switch to do stuff based on the child's name, and it was working just fine but I don't know what just happened I'm not getting it to work, (the tag on the player is working and assigned)...
private void OnTriggerStay(Collider car)
{
if (car.tag == "Player")
{
Transform[] children = GetComponentsInChildren<Transform>();
foreach (Transform child in children)
{
switch (child.name) //<-Also used this.name and was working fine
{
case "0":
player.transform.localPosition = new Vector2(-17, 9);
break;
case "1":
player.transform.localPosition = new Vector2(-16, 4);
break;
case "2":
player.transform.localPosition = dos;
break;
}
}…
Not getting an error it's just not doing anything :( help please
Answer by EXZ_EXZ · Jun 08, 2018 at 07:58 PM
Does your Car has a RigidBody attached to it and a collider set to "isTrigger" ?
BTW: Are you sure you want to do that? Right now you doing 120 checks on every physic update ...
Use Debug.Log to check if the collision and the tag is working fine. If not, you know where your problem is :)
Yes actually I had a Print("something"); on each case and it didn't work :( I don't get it the rigidbody and the colliders, tags are placed properly this gave me an idea I'm looking at this code
private void OnTriggerStay(Collider car)
{
print("Collider" + car.name);
And I'm not getting anything :( the script is active, the colliders have their triggers I'll try to check deeper, the colliders are placed in a proper layer and layer collision matrix seems ok
just to be sure:
your "player" needs a collider set as "isTrigger"
your player needs a rigidBody
your object needs to be a standart collider "not - isTrigger"
your object has the script attached to it
Set it up like this and try to get the name of the player a second time
Thanks, I kind of see what's happening but can't solve it, the Parent object has 120 child items that deter$$anonymous$$e your position in the map, the parent object has the script but no collider and the child objects have the colliders, I could simply put a script on the player and seek for these triggers but I want it to be the other way, I want that the triggers deter$$anonymous$$e if you are a player or an AI car and thus set your position in a $$anonymous$$imap, I'm able to see the child objects in the array but how do I use them inside the private void OnTriggerStay(Collider other)? That's why I had a switch based on the child name