- Home /
The question is answered, right answer was accepted
BroadcastMessage Null Reference Exception Problem
I have this piece of code here which is a part of an administrative console i am making. I tried to give the user a bit more freedom as to what type the BroadcastMessage Parameters are.
consoleInterpretation=consoleText.Split("."[0]);
if(consoleInterpretation[0]=="Find"){
if(consoleInterpretation.length==4){
var typeSplit2:String[];
typeSplit2=consoleInterpretation[3].Split(":"[0]);
if(typeSplit2.length==2){
if(typeSplit2[1]=="int")
GameObject.Find(consoleInterpretation[2]).BroadcastMessage(consoleInterpretation[2],int.Parse(typeSplit2[0]));
if(typeSplit2[1]=="float")
GameObject.Find(consoleInterpretation[2]).BroadcastMessage(consoleInterpretation[2],float.Parse(typeSplit2[0]));
if(typeSplit2[1]=="String")
GameObject.Find(consoleInterpretation[2]).BroadcastMessage(consoleInterpretation[2],typeSplit2[0]);
if(typeSplit2[1]=="GameObject")
GameObject.Find(consoleInterpretation[2]).BroadcastMessage(consoleInterpretation[2],GameObject.Find(typeSplit2[0]));
else
GameObject.Find(consoleInterpretation[2]).BroadcastMessage(consoleInterpretation[2],typeSplit2[0]);
}
else
GameObject.Find(consoleInterpretation[2]).BroadcastMessage(consoleInterpretation[2],consoleInterpretation[3]);
}
else
GameObject.Find(consoleInterpretation[1]).BroadcastMessage(consoleInterpretation[2]);
}
When i type "Find.MyGameObject.AddPoints.8:int"
i get the following error
NullReferenceException
UnityEngine.GameObject.BroadcastMessage (System.String methodName, System.Object parameter) (at C:/BuildAgent/work/842f9557127e852/Runtime/ExportGenerated/Editor/UnityEngineGameObject.cs:287)
A Debug.Log line i added showed me that typeSplit2[0]="8" and typeSplit2[1]="int" Any ideas as to what might be the problem?
EDIT: Now even if i type Find.MyGameObject.SampleFunction.text Where: SampleFunction(test:String) It gives a null reference exception
You might want to investigate the 'switch' statement for general program$$anonymous$$g coolness
Answer by DaveA · Sep 14, 2011 at 07:27 PM
You assume that whatever they type and ends up in consoleInterpretation[2] is going to be valid. I wouldn't. Do a Find on that, check the result for null (didn't find), and put up an error message if so. Then use that var you got back instead of consoleInterpretation[2] all over the place. In fact, check all user input for validity before trying to broadcast it.
Re 'typeSplit2[0]="8" and typeSplit2[1]="int" ': that's right. Split on "." gives you
[0] Find
[1] MyGameObject
[2] Addpoints
[3] 8:int
[3] split on ":" gives
[0] 8
[1] int
"Find.MyGameObject.SampleFunction.text Where: SampleFunction(test:String)" split on "." gives:
[0] Find
[1] MyGameObject
[2] SampleFunction
[3] text Where: SampleFunction(test:String)
and [3] split on ":" gives
[0] text Where
[1] SampelFunction(test
[2] String)
Checking back at my code i see that i am trying to find the gameobject by consoleInterpretation[2] which is the function's name. Would totally have missed it , thanks. Funny how its always these small damn things.
Follow this Question
Related Questions
BroadcastMessage Functionality Help 1 Answer
Send messages to different gameobjects 1 Answer
OnClickUpAsButton used externaly 0 Answers
Are message broadcasts instant? 1 Answer