Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Can't get OnTriggerExit to work.

Discussion in 'Scripting' started by Xsailia, Jun 19, 2018.

  1. Xsailia

    Xsailia

    Joined:
    Jun 19, 2018
    Posts:
    8
    So i'm making a game where there are 2 goals and i have to push all the objects to them. When they hit the goals they disappear and once all of them disappear it should load the next scene but i cant get it to work. The objects that disappear once they hit the goals are locked on Y axis so i put a cube with "is trigger" enabled on the same Y axis (It hit's all of them no matter where they are on the arena.) and then added my script to that cube but it doesn't work. No error's or warning's in the console either.
    Here's the script i use:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Gamefinish : MonoBehaviour {
    7.  
    8.     void OnTriggerExit (Collider other)
    9.     {
    10.         if (other.CompareTag("Ragdoll"))
    11.         {
    12.             Move(other);
    13.         }
    14.        
    15.     }
    16.  
    17.     void Move(Collider Ragdoll)
    18.     {
    19.         SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    20.     }
    21. }
    22.  
    All object are tagged as Ragdoll. I also added a picture so you can get an idea of the scenario.
    GamPic.PNG
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You should probably start by putting a debug in the OnTriggerExit and in the Move, and see if it's doing what you expect. There's no reason for an object to disappear, it just loads the next scene in Move, so any time an object leaves the trigger area, it should load the next scene.
     
    Xsailia and Doug_B like this.
  3. Xsailia

    Xsailia

    Joined:
    Jun 19, 2018
    Posts:
    8
    So i did what you suggested and no it does not do what i want. I changed my code to this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class Gamefinish : MonoBehaviour {
    7.  
    8.     void OnTriggerExit (Collider other)
    9.     {
    10.         Debug.Log("Next Scene!");
    11.     }
    12.    
    13. }
    Then i noticed that every time the player goes inside the goals (Is trigger activated) it activates the next scene but not when a ragdoll hits the goals even tho they disappear instantly. But even if i get this problem solved even then If one ragdoll hits the goals it would move to the next scene and i don't know how to make it so that it moves only if all ragdoll's are gone.
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Make sure the ragdoll is activating the trigger.
    If you scroll down on this page, you can see a matrix for collision/trigger: https://docs.unity3d.com/Manual/CollidersOverview.html

    Once you have the ragdoll properly causing the trigger, all that's left is keeping track of the count so you only change scenes when all are gone. :)
     
    Xsailia likes this.
  5. Xsailia

    Xsailia

    Joined:
    Jun 19, 2018
    Posts:
    8
    My ragdoll's wont activate the trigger even tho they disappear when they hit the goal's. And if I understood correctly according to the matrix it should work but it doesn't. And even if i got that working i don't know how to make it change scenes only when all of them are gone.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Just be keeping track of the number. If 5 are in the scene, you set a variable to 5, which is decreased with each ragdoll that's gone.
    Not sure what to say about the trigger not working if your setup matches a working part of the matrix.
    Do they get destroyed & it's not called? What if you move it out but don't destroy them - is it called then?

    Try to find what's wrong and fix it/work with/around it.
     
    whileBreak and Xsailia like this.
  7. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Can you show your script for how you are moving the objects? Are you doing so by transform.position or by rigidbody? Sometimes colliders/triggers tend to ignore collisions when they get set through direct position manipulation.
     
    Xsailia likes this.
  8. Xsailia

    Xsailia

    Joined:
    Jun 19, 2018
    Posts:
    8
    Thanks, I will try that. And yes they get destroyed but it wont activate the trigger.

    The red obstacles just have rigidbody and locked on Y axis. I'm moving the player by using addforce.
    Player movement script:
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class Player_Movement : MonoBehaviour {
    4.     public Rigidbody rb;
    5.     public float MovementForce = 2000f;
    6.  
    7.    
    8.     // Update is called once per frame
    9.     void FixedUpdate () {
    10.         if (Input.GetKey("w"))
    11.         {
    12.             rb.AddForce(0, 0, MovementForce * Time.deltaTime);
    13.         }
    14.         if (Input.GetKey("d"))
    15.         {
    16.             rb.AddForce(MovementForce * Time.deltaTime, 0, 0);
    17.         }
    18.         if (Input.GetKey("a"))
    19.         {
    20.             rb.AddForce(-MovementForce * Time.deltaTime, 0, 0);
    21.         }
    22.         if (Input.GetKey("s"))
    23.         {
    24.             rb.AddForce(0, 0, -MovementForce * Time.deltaTime);
    25.         }
    26.  
    27.     }
    28. }
     
  9. Xsailia

    Xsailia

    Joined:
    Jun 19, 2018
    Posts:
    8
    It work's now because i now teleport the objects away instead of destroying them but because i don't know much about coding i don't know how to keep track of how many objects have been moved and even if i could i don't know how to move to the next scene once all of them have been moved. Sorry for my stupidity :/

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ObjectEffect : MonoBehaviour {
    6.  
    7.     Vector3 tempPos;
    8.  
    9.     public GameObject DestroyEffect;
    10.  
    11.     public float count8;   // This is the variable that methos5k suggested but for some reason i cant put just 8 i have to have text in it.
    12.  
    13.     private void Start() {
    14.         tempPos = transform.position;
    15.     }
    16.    
    17.    
    18.     void OnTriggerEnter(Collider other)
    19.     {
    20.         if (other.CompareTag("Trigger"))
    21.         {
    22.             Destroy();
    23.         }
    24.     }
    25.     void Destroy()
    26.     {
    27.         Instantiate(DestroyEffect, transform.position, transform.rotation);
    28.  
    29.         tempPos.x += 50f;
    30.         transform.position = tempPos;
    31.     }
    32.  
    33. }
    34.  
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You also changed OnTriggerExit to Enter. You should be able to destroy them, and not just move them.

    Anyhow, as for keeping track of them, you could keep a script on a game object that has a count. Just make sure the count is set properly for the # you have spawned, and subtract one when they're moved/destroyed.
    Simply check if the count is <= 0, and if so change scenes.

    Do you know how to reference one script inside another?
     
    Xsailia likes this.
  11. Xsailia

    Xsailia

    Joined:
    Jun 19, 2018
    Posts:
    8
    Do you mean that i should create a new script on one of the objects or on the prefab to do the counting?
    I don't think so and sorry for my poor English.
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, well you should probably try to do some beginner tutorials from unity so you can get familiar with things & practice.
    When you understand some basic information, it will be easier to use that to build upon to make something of your own.
    Being able to reference 1 script in another is an easy and common task, for example.

    Maybe try here to get going: https://unity3d.com/learn/beginner-tutorials
     
    Xsailia and Doug_B like this.
  13. Xsailia

    Xsailia

    Joined:
    Jun 19, 2018
    Posts:
    8
    Thanks i will take a look at those!
     
  14. Xsailia

    Xsailia

    Joined:
    Jun 19, 2018
    Posts:
    8
    Hello! So sadly my Computer broke and i while it was repaired i lost my game. So now im saving for a new pc and when i have that i'll start over but thanks for the help anyways!