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. Dismiss Notice

Object not set to an instance of an object?

Discussion in 'Scripting' started by ThomasMartin, Oct 28, 2014.

  1. ThomasMartin

    ThomasMartin

    Joined:
    Feb 6, 2014
    Posts:
    56
    I am trying to access another script in another script and when I walk through the trigger I get the error displayed above. Can someone help me please. Here is the code
    Code (CSharp):
    1. void start(){
    2.         GameObject AlarmController = GameObject.Find("Alarms_Everything");// Finds the gameobject "Alarms_Everything"(This is where the Alarms script is found)
    3.         AlarmTriggered = AlarmController.GetComponent<Alarms>();//AlarmTriggered = Alarms Script.
    4.     }
    5.    
    6.         void OnTriggerEnter (Collider other){
    7.         if (other.gameObject.name == "Player") {
    8.             AlarmTriggered.alarmsActive = true;//alarmsActive = true;
    9.         }
    10.     }
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    In the start, are you sure that AlarmTriggered is getting set to something? Do the GameObject named 'Alarms_Everything' have an Alarms component added? Note, Find gives you the first game object of that name in the scene that it finds... maybe you have more than 1 GameObject named that and the one it's grabbing doesn't have an Alarms component.
     
  3. ThomasMartin

    ThomasMartin

    Joined:
    Feb 6, 2014
    Posts:
    56
    Yep, The Alarms script is attached to 'Alarms_Everything' and there is nothing else in my game called that.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    In the start function, put in a debug line that tests if AlarmTriggered was set.
     
  5. ThomasMartin

    ThomasMartin

    Joined:
    Feb 6, 2014
    Posts:
    56
    How would I do that? Debug.Log("?");
     
  6. ThomasMartin

    ThomasMartin

    Joined:
    Feb 6, 2014
    Posts:
    56
    Some help would be appreciated
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    You wrote 'start' instead of 'Start', thus Unity won't call the method here (unless it's named 'start' on purpose and you call 'start' in 'Start' or 'Awake').
     
  8. ThomasMartin

    ThomasMartin

    Joined:
    Feb 6, 2014
    Posts:
    56
    Ah!!! God these silly mistakes. Thanks!