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 darkmwar · May 30, 2014 at 12:46 PM · instantiatenullreferenceexceptioncloneclass objectmultiple-objects

NullReferenceException: Object reference not set to an instance of an object spawn.Update () (at Assets/spawn.cs:24)

I am fairly new to Unity and I am stuck. One of the problem I have is that if there are 2 different scripts, and both have a Start() and Update(), which scripts functions run first? hmmm.

And also im running in to this error, and I can't seem to figure it out: NullReferenceException: Object reference not set to an instance of an object spawn.Update () (at Assets/spawn.cs:24)

localVariables is a script for every clone objects created by//instantiate(object)

each object should have its own version of localVariables, meaning different index numbers

 public class localVariables : MonoBehaviour { 
     public static int index;
 
     void Start () { index = -1; } 
   
     public static int get(){
     return index;
     }
     public static void set(int a){
     index = a;
     }
     }

Then this script clone an object x amount of times and I want to save a number into the clone object's index variable:

 public class spawnBase : MonoBehaviour {
 
     
     public Transform baseSpawn;
     
 
     // Update is called once per frame
     void Update () {                    
                     
                     GameObject temp= Instantiate(baseSpawn,new Vector2(spawnObject.position.x,0.73f),spawnObject.rotation)as GameObject;
                     int index = globalScript.getFreeQueue();
                     //ERROR below this line
                     temp.gameObject.GetComponent<localVariables>().index = index;//line24 RUNTIME ERROR
                 
                 }}

Globalscript is just a script attached to an empty gameobject that holds globalVariables.

Can anyone point me in to the right direction please? Leave any comments and suggestions.

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

1 Reply

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

Answer by MohanadHamed · May 30, 2014 at 01:31 PM

You should not instantiate objects in Update() function, The update is called in each frame which means that you will create around 60 objects per second, this will lead to unexpected results.

You should move your code to a function called once like Start() or to other function called by you when needed.

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 darkmwar · May 31, 2014 at 07:30 AM 0
Share

That is not the problem though. That is part of the code striped down. I have flags of bools which is triggered but buttons. How do I fix that null problem thought ? :(

avatar image darkmwar · May 31, 2014 at 09:42 AM 0
Share

I found the actual error now, but I don't know why it is happening.

 GameObject temp= Instantiate(baseSpawn,new Vector2(spawnObject.position.x,0.73f),spawnObject.rotation)as GameObject;
                                     
                     if(temp==null){
                     Debug.Log("its not supposed to be null");
                 }

the object shows on both the camera and as a clone on the Hierarchy window, but it's null... what?!

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

21 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 avatar image avatar image avatar image avatar image

Related Questions

How can I assign a clone with a script to a variable? without drag & drop 1 Answer

my object instantiates to much 1 Answer

Instantiate Object Problem 0 Answers

Instantiating a random dropped consumable item from many cloned objects 1 Answer

initial velocity for instantiated ragdoll prefab 1 Answer


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