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

New to unity development, how can I use a trigger zone to get 2 objects to communicate?

Discussion in 'Scripting' started by Octuplex, Jun 11, 2018.

  1. Octuplex

    Octuplex

    Joined:
    Jan 23, 2017
    Posts:
    4
    I'm working on a game with mechanics that are functionally similar to something like diner dash, where the player has an object with variables and another object wants to check those variables against its own variables. I want this check to happen when the player enters a trigger zone, but I don't know how to do that without getting a nullReferenceException. Here's the C# script:
    Code (CSharp):
    1. void OnTriggerEnter (Collider other) {
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6. using UnityEngine.UI;
    7.  
    8. public class TriggerHandler : MonoBehaviour {
    9.  
    10.     //For detecting and dealing with the player being close enough to the booths to interact with them
    11.  
    12.     public GameObject otherGameObject;
    13.  
    14.     private bool inTheZone = false;
    15.  
    16.     void OnTriggerEnter(Collider other) {
    17.         if (other.tag == "Player")
    18.         {
    19.             inTheZone = true;
    20.             Text print = other.gameObject.GetComponent<Text>();
    21.             Debug.Log("Player Entered the Trigger" + print.text);
    22.         }
    23.  
    24.     }
    25.  
    26.     void OnTriggerStay(Collider other)
    27.     {
    28.         if (other.tag == "Player")
    29.         {
    30.             Debug.Log("Player is within the trigger");
    31.         }
    32.     }
    33.  
    34.     private void OnTriggerExit(Collider other)
    35.     {
    36.         if (other.tag == "Player")
    37.         {
    38.             inTheZone = false;
    39.             Debug.Log("Player has left the trigger");
    40.         }
    41.     }
    42.  
    43.     public bool isInTheZone()
    44.     {
    45.         return inTheZone;
    46.     }
    47.  
    48.  
    49. }
    50.  
    51.  
    52. }
    and here's the error I get:

    "NullReferenceException: Object reference not set to an instance of an object
    TriggerHandler.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Career fair/Scripts/TriggerHandler.cs:19)"
     
    Last edited: Jun 11, 2018
  2. Quasar47

    Quasar47

    Joined:
    Mar 24, 2017
    Posts:
    122
    Can you show us your whole script ? Supposing both your objects have the script TriggerHandler, and you want to get a string located in your script, you can do :

    Code (CSharp):
    1. void OnTriggerEnter (Collider other) {
    2.         if (other.tag == "Player")
    3.         {
    4.             inTheZone = true;
    5.          
    6.            if(other.gameObject.GetComponent<TriggerHandler>() == null)
    7.               return; // Those lines are optional
    8.  
    9.             string print = other.gameObject.GetComponent<TriggerHandler>().theStringVariableYouWantToGet;
    10.             Debug.Log("Player Entered the Trigger" + print);
    11.         }
    12. }
    I don't know your exact level with Unity nor what knowledge you have about it, but what you are trying to get with :

    Code (CSharp):
    1. other.gameObject.GetComponent<Text>();
    is a UI Component, so in order to get this component, you need to include at the top of your script :

    Code (CSharp):
    1. using UnityEngine.UI;
    If you've already done that, then your error comes from the fact that either the object you collided with doesn't have a tag "Player", or it doesn't have a Text Component. But once again, I would be more helpful if you showed me the rest of your code :)
     
  3. Octuplex

    Octuplex

    Joined:
    Jan 23, 2017
    Posts:
    4
    Okay, I updated the original post to include the whole script. didn't do it before because most of it is just standard imports.

    also, the player has 2 text components in its heirarchy. They aren't directly attached to the gameobject tagged as player, but they are attached to objects under the player. I've tried removing one of the text objects to see if it was a conflict but it didn't change anything.
     
  4. Quasar47

    Quasar47

    Joined:
    Mar 24, 2017
    Posts:
    122
    By "directly attached", do you mean you've created two new Text GameObjects and you parented them to the player (drag & drop on the player) ? If that's the case, it's normal if it doesn't work. GetComponent<>() will only work on the object which has this script as component. In your case, what you want is to get the childs of your player (that is to say the gameObjects under your player in the hierarchy). To get the Text Components you're looking for, you have two alternatives :

    - Either removing these childs and directly adding the Text Components onto your player GameObject.
    - Or, if you want to keep your child GameObjects, you can do this :

    Code (CSharp):
    1. void OnTriggerEnter(Collider other) {
    2.         if (other.tag == "Player")
    3.         {
    4.             inTheZone = true;
    5.             Text print = other.gameObject.transform.GetChild(0).gameObject.GetComponent<Text>();
    6.             Debug.Log("Player Entered the Trigger" + print.text);
    7.         }
    8.     }


    GetChild(0) will return the first gameobject under your player's hierarchy, so if your Text Component is on your first child, it should get it. And if you want to get the second text Component, just replace 0 by 1. Thses numbers are basically just indexes ; this function considers your player as an array of objects, and getChild will help you find the objects by their index in that array.

    Of course, you can also search a child object by its name by using :

    Code (CSharp):
    1. [code=CSharp]void OnTriggerEnter(Collider other) {
    2.         if (other.tag == "Player")
    3.         {
    4.             inTheZone = true;
    5.             Text print = other.gameObject.transform.Find("The name of your parented GameOobject").gameObject.GetComponent<Text>();
    6.             Debug.Log("Player Entered the Trigger" + print.text);
    7.         }
    8.     }
    [/code]


    but I wouldn't recommend it, in case you would like to change the name of your GameObject. It still works though, so feel free to pick up the solution you want. :)
     
  5. Quasar47

    Quasar47

    Joined:
    Mar 24, 2017
    Posts:
    122
    And sorry for my long answer, I like writing :)