• 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 Dreeka · Mar 08, 2011 at 06:52 PM · nullreferenceexception

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

Hello!

I am having this error message, when running the game. I am like to change a valubale in scriptEnemySpawn from scriptEnemyBlink. The problem is in the scriptEnemyBlink at enableSpawn = GetComponent(scriptEnemySpawn);.

scriptEnemyBlink:

//Insspector variables var blinkTime : float = 3.0; //the lifetime of the object var respawnWaitTime : int = 2; //When to respawn again var enableSpawn;

function Start() { blink(); //Starts the Blink script yield WaitForSeconds(respawnWaitTime); //Wait for an ammount of time destroy(); //Starts the destroy function. }

function blink() { var myRend:MeshRenderer = GetComponent(MeshRenderer); //Apply the MeshRenderer component to the myRend variable var myState:boolean = true; //Makes our state true while(true) //While our state is true { myState = !myState; //Change our state to the oposite myRend.enabled = myState; //Id our state is enabled, the renderer will be enabled. If our state is disabled, the rendeder will be disabled yield WaitForSeconds(0.25); //Waits for 0.25 seconds } }

function destroy() { enableSpawn = GetComponent(scriptEnemySpawn); enableSpawn.spawned = 1; Destroy(gameObject); //destroys our game object }

The scriptEnemySPawn script:

//Spawning script

//Inspector variables var blinkingSphereSpawnTime : int; //When to spawn the Blinking Sphere var blinkingSphere : Transform; //Add the blinking sphere prefab var blackBoxSpawnTime : int; //When to spawn the Black Box after the Blinking Sphere var blackBox : Transform; //Add the Black Box prefab var movingBoxSpawnTime : int; //When to spawn the Moving Box after the Black Box var movingBox : Transform; //Add the Moving Box prefab var spereRespawnTimeMax : int = 5; //The maximum ammount of time between the destruction and the spawning of a sphere var spawned : int; //Is the sphere spawned?

//Private variables: private var startPosition; //The starting position of the objects

function Start () { startPosition = Vector3(Random.Range(-6, 6), Random.Range(-4, 4), 0); //Generates a random spawning position yield WaitForSeconds(blinkingSphereSpawnTime); //Waits for an ammount of time Instantiate(blinkingSphere, startPosition, transform.rotation); //Creates the Blinking Sphere spawned = 1; //This is set to one, so the code in the function Update can start to run yield WaitForSeconds(blackBoxSpawnTime); //Waits for an ammount of time Instantiate(blackBox, startPosition, transform.rotation); //Creates the Black Box yield WaitForSeconds(movingBoxSpawnTime); //Waits for an ammount of time Instantiate(movingBox, startPosition, transform.rotation); //Creates the Moving Box }

function Update () { if (spawned == 1) //If the spere is spawned { if (GameObject.Find("prefabEnemySpereBlink(Clone)") == "null") //If there are no sphere spawned { spawn(); //Activate the spawn function spawned = 0; //Disable the code in the function update } } }

function spawn() {

 yield WaitForSeconds(Random.Range(0,spereRespawnTimeMax));      //Waits for a random ammount of time before spawning
 Instantiate(blinkingSphere, startPosition, transform.rotation); //Creates a new Blinking Spere
 spawned = 1;        //Re-enable the code in the function update

}

Thanks you for your response!

Comment
Add comment · Show 2
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 Justin Warner · Mar 08, 2011 at 11:25 PM 0
Share

scriptEnemyBlink isn't that long (53 lines)... Is their more you missed? Sorry.

avatar image Dreeka · Mar 10, 2011 at 04:03 PM 0
Share

Sorry, changed it.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

NullReferenceException: Object reference not set to an instance of an object. 2 Answers

Instantiate giving a NullReferenceError 1 Answer

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

MoveTowards doesn't work after changing tag. 2 Answers

NullPointerException while changing a variable from one script in another 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