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
-1
Question by UnitedMember · Jun 17, 2013 at 12:00 PM · errorinstantiatescoreclonesubtract

Object reference not set to an instance of an object

Hello, I'm pretty new to Unity and Java programming and I've been trying to make a score system.

I have a script called ScoreController and it looks like this :

 private var textScore : GUIText;
 public var Score : int;
 var Amount : int;
 
 function Awake()
 
 {
 
     textScore = GameObject.Find("TextScore").GetComponent(GUIText);
     
 }
 
 function Update()
 
 {
 
     textScore.text = "Score: " + Score.ToString();
  
 }
  
 function AddScore()
 
 {
 
     Score++;
 
 }
 
 function SubtractScore()
 
 {
 
     Score--;
 
 }
 
 function SetScore(Amount)
 
 {
 
     Score = Amount;
     
 }

And I'm using enemy object with script CountdownTimer :

 var scoreController : ScoreController;
 public var allowedTime : int = 5;
 private var currentTime = allowedTime;
 
 function Awake()
 
 {
 
     TimerTick();
  
 }
 
 function TimerTick()
 
 {
 
     while(currentTime > 0)
     
     {
 
         yield WaitForSeconds(1);
         currentTime--;
         
     }
     
     scoreController.GetComponent(ScoreController).SubtractScore();
     Destroy (this.gameObject);
     
 }

I want it to wait 5 seconds, subtract score and destroy itself. I also have other script that I found on google. It instantiates objects randomly so they all become clones of a prefab.

 var spawnPoints : Transform[];  // Array of spawn points to be used.
 var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
 var soundSpawn : AudioClip;
 var amountEnemies = 20;  // Total number of enemies to spawn.
 var yieldTimeMin = 0.5;  // Minimum amount of time before spawning enemies randomly.
 var yieldTimeMax = 1.5;  // Don't exceed this amount of time between spawning enemies randomly.
 var i = 5;
  
 function Start()
 {
     Spawn();
 }
  
 function Spawn() 
 { 
    for (i=0; i<10; i++) // How many enemies to instantiate total.
    {
       yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax));  // How long to wait before another enemy is instantiated.
  
       var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
       var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)];  // Randomize the spawnPoints to instantiate enemy at next.
       audio.PlayOneShot(soundSpawn);
  
       Instantiate(obj, pos.position, Quaternion.Euler(75, 0, 0)); 
       
       i=0;
    } 
 }

But whenever I run the game and 5 seconds pass I get the following error and the object doesn't destroy itself

Object reference not set to an instance of an object

This error happens because of this line:

 scoreController.GetComponent(ScoreController).SubtractScore();

I've been looking for how to correct it all over the place, however I was unable to solve it myself so I'm asking help from you guys. Please help.

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
0
Best Answer

Answer by KiraSensei · Jun 17, 2013 at 01:16 PM

In the enemy script, you define scoreController as of type ScoreController, but in line 26 you search the component ScoreController. This won't work.

You need to define scoreController as a game object, and in the inspector, put your game object that contains the first script into this variable (it is by default declared public, so there will be no problem).

Comment
Add comment · Show 3 · 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 UnitedMember · Jun 17, 2013 at 01:54 PM 0
Share

Thank you for your answer. Now the problem is I've made this enemy a prefab and I can't select a game object for scoreController variable unless I add the enemy to the scene.. Otherwise it just doesn't allow me to choose anything.. These enemies get spawned by other game object when the game starts, so there are none of them placed manually in the scene. Any tips?

avatar image KiraSensei · Jun 17, 2013 at 02:47 PM 0
Share

Sure, you can find your game object that contains the ScoreController script.

 scoreController = GameObject.Find("your game object name");
avatar image UnitedMember · Jun 17, 2013 at 03:23 PM 0
Share

It worked! Thank you so much!

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

15 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

Related Questions

Is there anything wrong with this script? It used to work but now it doesn't. 2 Answers

Instantiate Not Working - Boo Script 1 Answer

How to instantiate from prefab, not from instance? 3 Answers

Sometimes one sometimes two clone is formed. I just want to form a clone 1 Answer

Navmesh error on instantiated navmesh agents 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