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 Dreoh · Apr 16, 2012 at 12:52 AM · getcomponentreferencelayersfor-loop

Getting NullReferenceException? info inside

I made a GameObject array to keep track of the current enemies for easy referencing, however I cannot get it to work right. It seems like everything is in order, and I can compile and play, but I get the NullReferenceException error when I play.

I am trying to create a script for platforms that the characters can pass through the bottom of.

The three errors I get are

NullReferenceException: Object reference not set to an instance of an object EnemySpawn.Start () (at Assets/Custom Assets/Scripts/Enemy/EnemySpawn.js:18)

NullReferenceException: Object reference not set to an instance of an object Passable Platform.Update () (at Assets/Custom Assets/Scripts/Stage/Passable Platform.js:18)

NullReferenceException: Object reference not set to an instance of an object EnemySpawn.Start () (at Assets/Custom Assets/Scripts/Enemy/EnemySpawn.js:18)

And the Scripts are

------------------EnemySpawn.js--------------------

 var spawn : Transform;
 var spawnAtStart : boolean = false;
 var self : GameObject;
 var spawnedBool : boolean = false;
 var myClone : GameObject;
 var myCloneHealthScript : EnemyHealth;
 var cloneBool : boolean = true;
 var myCloneFollowScript : EnemyFollow;
 var player : GameObject;
 var StatisticsScript : Statistics;
 
 function Start(){
     
     player = GameObject.FindWithTag("Player");
     StatisticsScript = player.GetComponent(Statistics);
     
     for (var i = 0; i <= StatisticsScript.enemyCount.Length; i++)
     {
 
         if(StatisticsScript.enemyCount != GameObject) {
 
             StatisticsScript.enemyCount[i] = gameObject;
 
               return;
               
         }
 
     }
 
 }
 
 function Update () {
 
     if(spawnAtStart == true){
         spawnAtStart = false;
         spawnedBool = true;
         Spawn();
         }else{
             if(spawnedBool == false){
                 spawnedBool = true;            
                 Spawn();                        
             }
         }
 }
 
 
 
 
 function Spawn() {
     spawnedBool = true;
     
     myClone = Instantiate(self, spawn.position, Quaternion.identity);
     
     myClone.name = "Clone";
     
     myCloneHealthScript = myClone.GetComponent(EnemyHealth);
     
     myCloneHealthScript.cloneCheck = cloneBool;
     
     myCloneFollowScript = myClone.GetComponent(EnemyFollow);
     
     myCloneFollowScript.cloneCheck = cloneBool;
 }

--------------------------Passable Platform.js-------------------------------

 var StatisticsScript : Statistics;
 
 //***********************************************************************************************
 function Update () {
 
     player = GameObject.FindWithTag("Player");
     StatisticsScript = player.GetComponent(Statistics);
     
     /* if the player is made up of more than one collider, you'd
        need to iterate over the colliders and use playerBounds.Encapsulate()*/
        
     for(var i = 0; i <= StatisticsScript.enemyCount.Length; i++)
     {
     
         enemyBounds = StatisticsScript.enemyCount[i].collider.bounds;
         enemyCheck(enemyBounds, StatisticsScript.enemyCount[i]);
         
     }
        
     playerBounds = player.collider.bounds;
     playerCheck();
     
 
 }
 
 //***********************************************************************************************
 function playerCheck(){
 
     //check bottom of player to see if it is above the platform
     if(playerBounds.min.y >= collider.bounds.max.y)
     {
         Debug.Log("player is above platform");
         //make the platform solid
         Physics.IgnoreLayerCollision(8, 10, false);
         
     }else{
         Debug.Log("player is below platform");
         //turn off platform collision
         Physics.IgnoreLayerCollision(8, 10, true);
         
     }
 
 }
 
 //***********************************************************************************************
 function enemyCheck(enemyBounds : Bounds, enemy : GameObject){
 
     if(enemyBounds.min.y >= collider.bounds.max.y)
     {
     
         Physics.IgnoreCollision(collider, enemy.collider, false);
         
     }else{
     
         Physics.IgnoreCollision(collider, enemy.collider,true);
         
     }
 
 }

----------The relevant script from Statistics.js----------

 static var enemyCount : GameObject[];
Comment
Add comment · Show 7
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 Dreoh · Apr 16, 2012 at 01:02 AM 0
Share

oh, and the lines the errors point to are the for loops

avatar image IgnoranceIsBliss · Apr 16, 2012 at 03:48 AM 0
Share

Hard to tell - but the first and obvious question is "Does your Player object have a Statistics behaviour?"

If for some reason they don't, you'd get that exact error.

avatar image Dreoh · Apr 16, 2012 at 03:49 AM 0
Share

Yea it does, I checked thoroughly to make sure I had spellings right and everything. Thats why I posted on here

avatar image hijinxbassist · Apr 16, 2012 at 04:38 AM 0
Share

When does enemyCount on Statistics get populated? It is possible it is populated a frame or more after the call in Start() on EnemySpawn

avatar image syclamoth · Apr 16, 2012 at 05:31 AM 0
Share

Don't use static variables in Unity. They don't work the way you expect them to.

Show more comments

1 Reply

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

Answer by Atrius · Apr 16, 2012 at 05:40 AM

It's tough to say without seeing more, but are you instantiating your array, and if so is it happening before it's being used by these scripts? You define var enemyCount which is a builtin array that needs to be declared using:

 enemyCount = new GameObject[<size>];

Builtin arrays are a fixed size defined at that point and can't be resized. If that's not done before you begin accessing enemyCount you'll get an error like this.

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

9 People are following this question.

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

Related Questions

Referencing booleans from settings 1 Answer

getting a variable from another script problem 1 Answer

Reference Not Set toInstance of Object 1 Answer

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

Quick GetComponent syntax problem! 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