Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by romainover974 · Mar 22, 2017 at 10:34 AM · null reference exception

[Solved] Null Reference Exception on a text variable

Hi guys

I'm working on a script for a wave spawner when i lauch the game the script start i get this error :

NullReferenceException: Object reference not set to an instance of an object ZombieGameMode.Update () (at Assets/Mes Asset/Script/JeuInfo/ZombieGameMode.js:50)

I've checked in the Inspector and both of the variable are set i've try a lot of thing and it doesn't fix it (Sorry for my english i'm frebch btw)

Here is my script :

 #pragma strict
 import UnityEngine.UI;
 
 var Second : int  = 00;
 var Minute : int = 00;
 var Wave : int = 00;
 var WaveTimer : int = 00 ;
 var boucle : boolean = false;
 var TimerUI : Text ;
 var WaveUI : Text ;
 var ennemy1 : GameObject;
 
 
 private var spawnSpawner : int ;
 private var SpawnNumber : int ;
 var Spawn : int ;
 var SpawnCount : int = 10 ; 
 public var AllSpawner : GameObject[];
 
 
 function Start () {
     AllSpawner = GameObject.FindGameObjectsWithTag("Spawner"); ;
     SpawnNumber = AllSpawner.length ;
 }
 
 
 function Update () {
 
 
     //Veriffier toute les frame les seconde 
     if (boucle == false){
         Secondcount();
         boucle= true ;
     }
 
     if(Second == 60){
         Minute = Minute + 1;
         Second = 0 ;
     }
 
     if(Minute == WaveTimer ){
         WaveTimer = WaveTimer +1;
         Wave = Wave + 1 ;
         SpawnCount = SpawnCount*1.5 ;
         Spawns();
     }
 
 
     //Ubdate le time
     TimerUI.text= Minute+" : "+Second;
     //Ubdate les wave 
     WaveUI.text = "Wave : "+ Wave ;
 
 
 }
 
      
            
    
 
 
 function Spawns(){
     spawnSpawner = SpawnCount/SpawnNumber ;
     for(var i=0; i < spawnSpawner;i++){
     for(var Place : GameObject in AllSpawner){
         yield WaitForSeconds(1);
         var position = Place.transform.position ;
         Instantiate (ennemy1,position,transform.rotation) ;
     }
         
     }
 }
 
 
 //Ajouter une seconde toute les seconde
 function Secondcount(){
     Second = Second + 1 ;
     yield WaitForSeconds(2);
     boucle = false ;
 }
 
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 romainover974 · Mar 22, 2017 at 10:36 AM 0
Share

And i've forgot to says that the game start and the scripts seems to work but i still get this error in the console .

1 Reply

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

Answer by RecyclingBen · Mar 22, 2017 at 11:31 AM

Looks like something in your script is trying to reference something that does not exist (YET!!)

It's completely possible that what it is trying to reference has not been created in the first couple frames of the game, so you get the error at the start of it.

It's good practice to check if a referenced object != null before using it (I'm a complete hypocrite on that)

So if your game is working 100% fine just use an if statement like

if (objectImReferencing != null) { use objectImReferencing normally in here}

that way even if the object isn't created yet you don't get a NullReferenceException

Comment
Add comment · Show 1 · 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 romainover974 · Mar 22, 2017 at 12:26 PM 0
Share

Thanks you that worked for me for people how want an exemple i've changed

      //Ubdate le time
      TimerUI.text= $$anonymous$$inute+" : "+Second;
      //Ubdate les wave 
      WaveUI.text = "Wave : "+ Wave ;
  

To :

     if (TimerUI == !null &&  WaveUI == !null ){
         //Ubdate le time
         TimerUI.text= $$anonymous$$inute+" : "+Second;
         //Ubdate les wave 
         WaveUI.text = "Wave : "+ Wave ;
 
     }



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

97 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 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 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 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 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

NullReferenceException: Object reference not ... ( Assistance please ) 2 Answers

"NullReferenceException: Object reference not set to an instance of an object GameHelper.Update () (at Assets/Scripts/GameHelper.cs:44)" 2 Answers

Vector3 array causing nullreferenceException 1 Answer

null reference help. i fundamentally dont understand. 1 Answer

why does a line renderer cause a nullreference excepetion? 0 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