Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Collider List - onTriggerStay

Discussion in 'Scripting' started by Mr_Albert, Jun 28, 2017.

  1. Mr_Albert

    Mr_Albert

    Joined:
    Apr 28, 2017
    Posts:
    25
    Hello pro... I have a new question for you.

    I create a colliders for check a status but not working.

    In Start I try to create a list for the object:
    Code (CSharp):
    1.  
    2.  
    3.        void Start ()
    4.        {
    5.  
    6.            //TOP COVER VISOR
    7.            // load prefabs and put into blablabla e blablabla
    8.            GameObject CoverVisor_Top = (GameObject)Instantiate(Resources.Load("coverVisor"));
    9.            CoverVisor_Top.name = "CoverVisor_Top";
    10.            CoverVisor_Top.transform.parent = GetVar.PlayerContainer.transform;
    11.            Vector3 CoverVisor_Top_Position = new Vector3(GetVar.PlayerContainer.transform.position.x, GetVar.PlayerContainer.transform.position.y+1.5f, GetVar.PlayerContainer.transform.position.z+0.3f);
    12.            CoverVisor_Top.transform.position = CoverVisor_Top_Position;
    13.  
    14.            //CENTER COVER VISOR ...
    15.            //CENTERLEFT COVER VISOR ...
    16.            //CENTERRIGHT COVER VISOR ...
    17.  
    18.            // create a list
    19.            List<GameObject> Triggers;
    20.            Triggers = new List<GameObject>();
    21.  
    22.            //add to list "Triggers" the triggers
    23.            Triggers.Add(CoverVisor_Top);
    24.            Triggers.Add(CoverVisor_Center);
    25.            Triggers.Add(CoverVisor_CenterLeft);
    26.            Triggers.Add(CoverVisor_CenterRight);
    27.  
    28.           ...
    29.  
    30.  
    31.        }
    32.  


    And in the colliders events I try to check if the name is correct:
    Code (CSharp):
    1.  
    2.  
    3.        void OnTriggerStay(Collider Triggers)
    4.        {
    5.  
    6.            if (Triggers.gameObject.name == "CoverVisor_Center")
    7.            {
    8.                GetVar.CoverVisor_Center_Status = true;
    9.            }
    10.            else{
    11.                GetVar.CoverVisor_Center_Status = false;
    12.            }
    13.  
    14.        }
    15.  
    16.  

    The debug not talk about any errors... and i not have experience for see the problem... can help me? thanks!
     
    Last edited: Jun 28, 2017
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Ok, can you just place these objects in the scene instead?

    Are you trying to compare the parameter from the OnTriggerStay to the list you have in the first script? That's not possible how you've done it.

    I'm a little confused about what you're doing in general.
     
  3. Mr_Albert

    Mr_Albert

    Joined:
    Apr 28, 2017
    Posts:
    25
    Hi @methos5k.

    Yes. I created the objects to see if they collide with the wall.

    I do not know how to get them into OnTriggerStay after having put them on scene.
    I thought I had to put them in a list, put them into ontrig and compare the name of the object to verify the collision.

    As you can see I have some difficulties ...
    What am I doing wrong?
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, in order to detect a collision, there must be a collider + a rigidbody. For triggers (not collisions), the rigidbody can be kinematic.
    Then, the physics system will report the callback. In there, you can check the name of the method parameter, similar to what you're doing.
    Just ensure that the setup involves what I described. Colliders on both objects involved in the trigger, and 1 (or both) with a rigidbody.
    You also don't need a list for this, unless you're using the list somewhere else. However, if you do plan to use the list somewhere else, you cannot declare it in Start() because then its scope is only until the end of the Start function, and it doesn't exist any other time.
     
  5. Mr_Albert

    Mr_Albert

    Joined:
    Apr 28, 2017
    Posts:
    25
    ok.

    I change the script:

    ONSTART
    Code (CSharp):
    1.  
    2.  
    3.        void Start ()
    4.        {
    5.  
    6.            //TOP COVER VISOR...
    7.            //CENTER COVER VISOR...
    8.            //CENTERLEFT COVER VISOR...
    9.            //CENTERRIGHT COVER VISOR....
    10.       }
    11.  
    12.  
    ONTRIGEVENT
    Code (CSharp):
    1.  
    2.      
    3.        void OnTriggerStay(Collider Triggers) {
    4.  
    5.            if (Triggers.gameObject.name == "CoverVisor_Top")
    6.            {
    7.                GetVar.CoverVisor_Top_Status = true;
    8.            }
    9.            if (Triggers.gameObject.name == "CoverVisor_Center")
    10.            {
    11.                GetVar.CoverVisor_Center_Status = true;
    12.            }
    13.        }
    14.  
    15.  

    Now... I am confuse.
    All object have collider and rigid body but work only whit the object has the script inside.
    If I move the mesh and touch the playercontroller (which has the script inside) it work and the bool change, if move the mesh and touch other not work.

    I have question: "Is it relative to script impact and not the object selected?" and "If yes, how i can select the object for check the OBJECT impact respect EVERY OTHER OBJECT impact?"


    thanks....
     
    Last edited: Jun 29, 2017
  6. Mr_Albert

    Mr_Albert

    Joined:
    Apr 28, 2017
    Posts:
    25
    1. Exemple:

      if (the collider mesh of CoverVisor_Top touch something)
    2. {
    3. GetVar.CoverVisor_Top_Status = true;
    4. }


      Otherwise it seems

    5. if (the this mesh whit the script has touched by CoverVisor_Top)
    6. {
    7. GetVar.CoverVisor_Top_Status = true;
    8. }
     
    Last edited: Jun 29, 2017
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sorry, I couldn't completely understand what you're asking.

    If you scroll down on this page , you can see a matrix of collision & trigger setups: https://docs.unity3d.com/Manual/CollidersOverview.html

    If two objects collider and 1 is a trigger, and so long as you have a collider and rigidbody, it doesn't matter which object has the script. The only part that matters, obviously, is what you check for with the parameter.

    By the way, the most recent code you posted shows you using OnTriggerExit as opposed to OnTriggerStay. Just checking that was on purpose.
     
  8. Mr_Albert

    Mr_Albert

    Joined:
    Apr 28, 2017
    Posts:
    25
    Yes, i wrong the post...

    ok... thanks.