Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by asduffo · Jul 04, 2013 at 02:10 PM · randomnullreferenceexceptionloopsendmessage

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:alt text

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

erroreunity.png (7.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image asduffo · Jul 04, 2013 at 04:04 PM 0
Share

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?

avatar image Sisso · Jul 04, 2013 at 04:47 PM 1
Share

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;
 }
avatar image asduffo · Jul 05, 2013 at 01:44 PM 0
Share

No since the circles are prefabs, so transforms, not gameobject like target

avatar image
1

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image asduffo · Jul 04, 2013 at 02:47 PM 0
Share

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

avatar image aldonaletto · Jul 05, 2013 at 12:10 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Trouble with Null Reference in Script 2 Answers

Creating a script to randomize events. 1 Answer

Pick random song at the biggining and then loop it the rest of the game 1 Answer

Why is OnGUI not working? 1 Answer

SOLVED! - Pick Number != These Other Numbers 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges