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 ezzedine114 · Oct 21, 2013 at 03:54 AM · accessactivationinsantiate

NullReferenceException: Object reference not set to an instance of an object

hello guys !

i have a game object ( an ennemy) that is instansiated 1 second after the start of the game from a prefab.

with this code

 #pragma strict
 // this is the object(prefab) i am instantiating
 public var zombprefab: Rigidbody;
 var ennemies: GameObject[];
 private var temps : float = 1f;
 public var spawnV : float = 7f;
 
 
 
 
 
 // this is the position on the scene to wich the zombie spawn
 public var spawn: Transform;
 
 function Start () 
 {
 
 }
 
 function Update () 
     {
     // after 1 second of entering the game zombies are instantiated on the scene as long as they are less or equal to 10 
     // the instantiation rate define by the variable spawnV is of one zombie every 7 second
       if(temps < Time.time && ennemies.Length < 10)
             {
             var zombieinstance: Rigidbody;
             zombieinstance = Instantiate(zombprefab, spawn.position, spawn.rotation);
             temps = Time.time + spawnV;
             }
             
         // i keep track of the number of ennemies on the scene by storing their number into the ennemies variable
         ennemies = GameObject.FindGameObjectsWithTag("ennemie");
         
                 
             
             
     
     }

that zombie prefab that is istantiated , have a script that i am trying to access like fallow

  #pragma strict
     
         public var jumpspeed = 11f;
         public var movespeed = 10f;
         private var robcollider : CapsuleCollider;
         var anim : tk2dSpriteAnimator;
         var zombie : ZOMBIE;
         
         
         
         
         
         
         
         function Start () {
         
      anim = GetComponent(tk2dSpriteAnimator);
      robcollider = GetComponent(CapsuleCollider);
 // HERE I AM TRYING TO ACCESS THE SCRIPT THAT IS ATTACHED TO THE INSTANTIATED PREFAB
      zombie = GameObject.Find("Zombie").GetComponent(ZOMBIE);
      }

but i receive the fallowing error : NullReferenceException: Object reference not set to an instance of an object .

i understand it is because i am triying to access the script on the a gameobject that is not yet instantiated at the start of the game.

but my question is , how can i overcome this issue thanks in advance.

Comment
Add comment · Show 1
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 Benproductions1 · Oct 21, 2013 at 04:03 AM 0
Share

Where is the error?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Azrapse · Oct 21, 2013 at 05:08 AM

If the zombie is spawned 1 second after the game starts, but you want your following script to start following 0 seconds after the game starts... well, I guess you see there is an inconsistency there. What is the follower script supposed to follow from second 0 to second 1?

If your zombie must spawn some time after game start, you have no other choice but make the follower script wait until there is a zombie to follow. Instead of having that last line in the Start() method of the follower script, remove it and have this in Update().

 function Update(){
     if(!zombie){
         var zombieObject = GameObject.Find("Zombie");
         if(zombieObject){
             zombie = zombieObject.GetComponent(ZOMBIE);
         }
     }
 
     if(zombie){
         // Do here the things that this follower script is supposed to do when it has found the zombie...
     }
 }
Comment
Add comment · 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

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How should I describe to access child mesh in Java 1 Answer

accessing instance of a script 1 Answer

How do I script a prefab like I would code a class? 1 Answer

Turning off single object in an array 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