- Home /
SendMessage weird runtime error
bad engRish incoming
Hi everyone.
I asked sometime ago help for a problem that kept me busy for a while, and now i managed to solve it.
But now i have another problem with the same project: basically what i want is render 2 figures at random position: that's the script portion where i render it:
//other code here. xPos, yPos and posizione are declared before.
for (int i = 0; i < 2; i++)
{
xPos = Random.Range(-55, 55);
yPos = Random.Range(-75, 60);
posizione = new Vector3(xPos, xPos, 5);
renderizzabile = Instantiate(listaOggetti[i], posizione, Quaternion.identity) as GameObject;
inGioco.Add(renderizzabile);
}
If i run the game, it starts as expected: i have 2 sprites rendering themselves at a random position in the screen without problem. But now: since i need that one of the 2 shape have to do a particular thing, i added a SendMessage inside the loop, and now the code is like that:
for (int i = 0; i < 2; i++)
{
xPos = Random.Range(-55, 55);
yPos = Random.Range(-75, 60);
posizione = new Vector3(xPos, xPos, 5);
renderizzabile = Instantiate(listaOggetti[i], posizione, Quaternion.identity) as GameObject;
if (i == figuraOk)
{
renderizzabile.SendMessage("setFiguraTarget", true);
}
inGioco.Add(renderizzabile);
}
Uh i forgot: figuraOk (declared before) is just a random number between 0 and 1.
SetFiguraTarget, declared inside another script contained in all of the prefabs declared inside the list listaOggetti, is just a setter wich sets a bool on true. The same script contains also an OnMouseDown method, so if i click on an object, according to the value of the bool setted with SetFiguraTarget, it prints "ok" or "no" but i doubt it's important).
So: i run the game with sendMessage added to the script and that's the result:
An endless list of circles (a circle is one of the 2 shapes in the list) appaers in diagonal. The strange thing is also that their x and y position in the plane is now the same (in a shape x and y are both 32, in another -52 etc), so not anymore randomly.
Also, for every shape rendered, the console shows me that error:
NullReferenceException: Object reference not set to an instance of an object Prova3.Update () (at Assets/script/ingame/Prova3.cs:73)
Since i really don't know what could be the error, could somebody help me?
Regards, asduffo
Answer by aldonaletto · Jul 04, 2013 at 03:06 PM
The X and Y coordinates are the same due to a typo in the line below:
posizione = new Vector3(xPos, xPos, 5);
You're passing xPos to X and Y!
About the other error: maybe the cause is SendMessage being called right after Instantiate - by that time, the new object hasn't executed the Start functions in its scripts yet: if a reference variable is set at Start, it's null at this point. If this is the case, try to move the initialization to Awake: I'm not sure, but suppose that Awake is executed before Instantiate returns.
true on the fisrt answer. As for the second, i doubt since i didn't use those variables on start. Is the same if i use thread.sleep for a couple of milliseconds so the figure can initialize himself well? Or is my reasoning wrong?
Use yield or thread.sleep generally is not a good options. Alternatively, if you don't want to have the dependency with GameObject lifecycle, you could resolve your references on demand.
Ins$$anonymous$$d :
var target: GameObject;
function Start() {
target = GameObject.Find("Target");
}
Could use:
var target: GameObject;
function GetTarget(): GameObject {
if (target == null) target = GameObject.Find("Target");
return target;
}
No since the circles are prefabs, so transforms, not gameobject like target
Answer by Sisso · Jul 04, 2013 at 02:40 PM
Look at your error:
NullReferenceException: Object reference not set to an instance of an object Prova3.Update () (at Assets/script/ingame/Prova3.cs:73)
NullReferenceException - You are acessing a variable with empty/null/not defined value
Prova3.Update - In class Prova3, method Update
Assets/script/ingame/Prova3.cs:73 - The file and line that cause exception.
it was an example: the error is in the line renderizzabile = Instantiate(listaOggetti[i], posizione, Quaternion.identity) as GameObject;
shown above. The error appaers for anyone of the circles shown in the image
Are you sure that the error is in this line? This could only happen if listaOggetti had null values - or if it was uninitialized.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105457im_/https://answers.unity.com/themes/thub/images/avi.jpg)