Search Unity

Script returns null reference but debug logtells me otherwise (solved)

Discussion in 'Scripting' started by tawdry, Jan 26, 2015.

  1. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Hi Guys its me again
    So i set up a trigger to get names of object entering it and then access the components but it refuses to play ball!, Anyone able to see why or suggest a method that will work.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class trap : MonoBehaviour {
    5.  
    6.     Animator dark;
    7.     string bc="";
    8.     GameObject de;
    9.  
    10.  
    11.  
    12.     void OnTriggerEnter(Collider other){
    13.  
    14.         if (other.name !=null)
    15.         {     bc = other.name;
    16.             de = GameObject.Find (bc);
    17.             Debug.Log (bc);
    18.             dark = de.GetComponent<Animator>();
    19.         dark.runtimeAnimatorController = Resources.Load ("explode") as RuntimeAnimatorController;
    20.             }
    21.  
    22.     }
    23.  
    24. }
    25.    
    The debug log states the correct name of the object (knight)
    but de = GameObject.Find (bc); returns null
     
    Last edited: Jan 26, 2015
  2. Random_Civilian

    Random_Civilian

    Joined:
    Nov 5, 2014
    Posts:
    55
    For one, you are looking for the game object with the name "bc" not for the game object with the name held by bc. Second of all,
    Code (CSharp):
    1. de = other.gameObject;
    would be more efficient.
     
  3. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Yea i tried that as well also return a null even if i put in the actual name it returns a null i can't see what i'm missin.
     
  4. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Try this

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class trap : MonoBehaviour {
    4.     Animator dark;
    5.  
    6.     void OnTriggerEnter(Collider other){
    7.         if (other.name !=null)
    8.         {  
    9.             GameObject de = other.collider.gameObject;
    10.             Debug.Log (de.name);
    11.             dark = de.GetComponent<Animator>();
    12.         dark.runtimeAnimatorController = Resources.Load ("explode") as RuntimeAnimatorController;
    13.             }
    14.     }
    15. }
     
  5. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    Awesome thx guys its working now aaro with your script change . Man that was a annoying 2 hours thx for saving some of my sanity.
     
    Last edited: Jan 26, 2015
  6. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    490
    Also, the name can never be null, If you want it to say if the name isn't "" then say

    if(other.name != ""){

    }
     
  7. tawdry

    tawdry

    Joined:
    Sep 3, 2014
    Posts:
    1,357
    It does work with the null query though weird.