Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

UnassignedReferenceException with box collider trigger

Discussion in 'Scripting' started by woahitsjc, Jul 16, 2016.

  1. woahitsjc

    woahitsjc

    Joined:
    Jul 16, 2016
    Posts:
    5
    So I have a platform spawner spawning 10 platforms(SpawnPlatform script) with a 2d box collider attached so when the ball enters, it instantiates the object a little futher away from the trigger (TriggerCheck script). However I am getting an UnassignedReferencedException when the ball enters the trigger. It states that my variable object in the TriggerCheck script has not been assigned.
    Here is what I got for the main part of the scripts, any advice to solve this problem would be great. Thanks!

    Platform Spawn
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class EndlessPlatform : MonoBehaviour {
    6.  
    7.   public GameObject platform;
    8.   public GameObject jumpBox;
    9.   Vector2 lastPos;
    10.   float size;
    11.   public bool gameOver;
    12.  
    13.   // Use this for initialization
    14.   void Start () {
    15.      lastPos = platform.transform.position;
    16.      size = platform.transform.localScale.x;
    17.  
    18.      for(int i = 0; i < 10; i++) {
    19.         SpawnPlatforms();
    20.      }
    21.   }
    22.  
    23.    // Update is called once per frame
    24.    void Update () {
    25.  
    26.    }
    27.  
    28.   void SpawnPlatforms() {
    29.      Vector2 pos = lastPos;
    30.      pos.x += size;
    31.      lastPos = pos;
    32.  
    33.      Instantiate(platform, pos, Quaternion.identity);
    34.      }
    35. }
    36.  
    TriggerCheck
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class TriggerCheck : MonoBehaviour {
    6.  
    7.   public GameObject jumpBox;
    8.   public GameObject slideBox;
    9.  
    10.   Vector2 lastPos;
    11.   float size;
    12.  
    13.   // Use this for initialization
    14.   void Start () {
    15.      lastPos = transform.parent.position;
    16.      size = transform.parent.localScale.x;
    17.   }
    18.  
    19.    // Update is called once per frame
    20.    void Update () {
    21.  
    22.    }
    23.  
    24.   void OnTriggerEnter2D(Collider2D col) {
    25.      Vector2 pos = lastPos;
    26.      pos.x += size;
    27.      lastPos = pos;
    28.  
    29.      int rand = Random.Range(0, 20);
    30.      if (col.gameObject.tag == "Player" && rand < 11) {
    31.         Instantiate(jumpBox, new Vector2(pos.x + 2, pos.y + 0.7f), Quaternion.identity);
    32.      }
    33.   }
    34.  
    35.   void OnTriggerExit2D(Collider2D col) {
    36.      if (col.gameObject.tag == "Player") {
    37.      Destroy(transform.parent.gameObject, 5f);
    38.      }
    39.   }
    40. }
    41.  
     
  2. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    assign something to the variable in question...
     
  3. woahitsjc

    woahitsjc

    Joined:
    Jul 16, 2016
    Posts:
    5
    Got it, my prefab wasn't the same as my object in the scene so the script was using the object in the scene instead.
     
  4. Deleted User

    Deleted User

    Guest

    i don't know bro same problem here
     
unityunity