- Home /
C# Null Reference Exception in Custom Function
Hi everyone, I'm trying to create a function that a finds a gameobject with a tag and then adds the gameobject's children to a transform array. When I run the script the console gives me a null reference exception and it points to line 5. Any reason why? I don't see anything that is null here.
public Transform[] someTransformArray;
void FindGameObjectsChildrenByTag(string tag, Transform[] transformArray){
GameObject go = GameObject.FindGameObjectWithTag(tag);
transformArray = go.GetComponentsInChildren<Transform>();//Null Reference Exception
}
void Awake(){
FindGameObjectsChildrenByTag("someTag", someTransformArray);
}
Answer by iwaldrop · Jun 27, 2013 at 01:02 AM
It's very likely that GameObject go is null. You should always do a null check before using a value that you think could be null. Think to yourself, what happens if GameObject.FindGameObjectWithTag doesn't find anything?
GameObject go = GameObject.FindGameObjectWithTag(tag);
if (go != null)
transformArray = go.GetComponentsInChildren<Transform>();
else if (Debug.isDebugBuild)
Debug.LogError(string.Format("No GameObject with tag {0} was found.", tag));
I found out that I didn't have it tagged correctly and now I'm not getting a null reference. But now when I run my script someTransformArray doesn't equal go's children.
public Transform[] someTransformArray;
void FindGameObjectsChildrenByTag(string tag, Transform[] transformArray){
GameObject go = GameObject.FindGameObjectWithTag(tag);
if (go != null)
transformArray = go.GetComponentsInChildren<Transform>();
else if (Debug.isDebugBuild)
Debug.LogError(string.Format("No GameObject with tag {0} was found.", tag));
}
void Awake(){
FindGameObjectsChildrenByTag("someTag", someTransformArray);// Nothing Happens
}
Is there anything in it? It should be an array of all transforms under and including the GameObject that you attach the script to. Print them all out...
Transform[] ts = GetComponentsInChildren<Transform>();
foreach (Transform t in ts)
Debug.Log(t.name, t);
You never assign transformArray to someTransformArray. You should either use the ref keyword on your transformArray property, or don't pass it at all and have your function set someTransformArray directly. You should also properly indent your code.
I don't understand what you're saying. Of course someTransformArray doesn't contain anything, because you never assign anything to it (like I said in my previous comment). I don't know if you want it to be set or not now, but I've told you what to do it you do want it set.
How do you use the ref keyword? I tried putting it next to transformArray and I'm getting an error from the console saying I have invalid arguments
void FindGameObjectsChildrenByTag(string tag, ref Transform[] transformArray){
GameObject go = GameObject.FindGameObjectWithTag(tag);
if (go != null)
transformArray = go.GetComponentsInChildren();
else if (Debug.isDebugBuild)
Debug.LogError(string.Format("No GameObject with tag {0} was found.", tag));
}
Your answer
Follow this Question
Related Questions
Variables Set by Function Outside of Script 1 Answer
C# Textfield Null Reference Exception 1 Answer
NullReferenceException After A Few Hours? 1 Answer
NullReferenceException: Object reference not set to an instance of an object 1 Answer
Working Reference Still Throwing Null Reference Error 0 Answers