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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by jazman3d · Oct 07, 2014 at 07:27 PM · c#tutorialspace shooter

Project: Space Shooter - can't assign variable scoreText

Hello, I'm following the Space Shooter tutorial and keep running into a problem. I can't assign a variable to scoreText in the GameController script. First it seems that the tutorial is dated as I'm on Unity 4.60b20 on a Mac Pro and there isn't a GUI Text option under Hierarchy>Create. I created a UI>Text instead and try to drag and drop this into the scoreText field of the GameController script but Unity doesn't let me. This the error I get:

UnassignedReferenceException: The variable scoreText of GameController has not been assigned. You probably need to assign the scoreText variable of the GameController script in the inspector.

Here are the two scripts I'm using:

 using UnityEngine;
 using System.Collections;
 
 public class GameController : MonoBehaviour
 {
     public GameObject hazard;
     public Vector3 spawnValues;
     public int hazardCount;
     public float spawnWait;
     public float startWait;
     public float waveWait;
     
     public GUIText scoreText;
     private int score;
     
     void Start ()
     {
         score = 0;
         UpdateScore ();
         StartCoroutine (SpawnWaves ());
     }
     
     IEnumerator SpawnWaves ()
     {
         yield return new WaitForSeconds (startWait);
         while (true)
         {
             for (int i = 0; i < hazardCount; i++)
             {
                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                 Quaternion spawnRotation = Quaternion.identity;
                 Instantiate (hazard, spawnPosition, spawnRotation);
                 yield return new WaitForSeconds (spawnWait);
             }
             yield return new WaitForSeconds (waveWait);
         }
     }
     
     public void AddScore (int newScoreValue)
     {
         score += newScoreValue;
         UpdateScore ();
     }
     
     void UpdateScore ()
     {
         scoreText.text = "Score: " + score;
     }
 }


And the second:

 using UnityEngine;
 using System.Collections;
 
 public class DestroyByContact : MonoBehaviour
 {
     public GameObject explosion;
     public GameObject playerExplosion;
     public int scoreValue;
     private GameController gameController;
     
     void Start ()
     {
         GameObject gameControllerObject = GameObject.FindWithTag ("GameController");
         if (gameControllerObject != null)
         {
             gameController = gameControllerObject.GetComponent <GameController>();
         }
         if (gameController == null)
         {
             Debug.Log ("Cannot find 'GameController' script");
         }
     }
     
     void OnTriggerEnter(Collider other) 
     {
         if (other.tag == "Boundary")
         {
             return;
         }
         Instantiate(explosion, transform.position, transform.rotation);
         if (other.tag == "Player")
         {
             Instantiate(playerExplosion, other.transform.position, other.transform.rotation);
         }
         gameController.AddScore (scoreValue);
         Destroy(other.gameObject);
         Destroy(gameObject);
     }
 }

Is there are way around this? I'm not a C# programmer and apologize at the noob question. Thank you.

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

5 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by jazman3d · Oct 08, 2014 at 05:39 AM

Thanks awest, I finally got it working by adding the UnityEngine.UI and using the bit of code you suggested:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class GameController : MonoBehaviour
 {
     public GameObject hazard;
     public Vector3 spawnValues;
     public int hazardCount;
     public float spawnWait;
     public float startWait;
     public float waveWait;
     
     public Text scoreText;
     private int score;
     
     void Start ()
     {
         scoreText = GetComponent <Text> ();
         score = 0;
         UpdateScore ();
         StartCoroutine (SpawnWaves ());
     }
     
     IEnumerator SpawnWaves ()
     {
         yield return new WaitForSeconds (startWait);
         while (true)
         {
             for (int i = 0; i < hazardCount; i++)
             {
                 Vector3 spawnPosition = new Vector3 (Random.Range (-spawnValues.x, spawnValues.x), spawnValues.y, spawnValues.z);
                 Quaternion spawnRotation = Quaternion.identity;
                 Instantiate (hazard, spawnPosition, spawnRotation);
                 yield return new WaitForSeconds (spawnWait);
             }
             yield return new WaitForSeconds (waveWait);
         }
     }
     
     public void AddScore (int newScoreValue)
     {
         score += newScoreValue;
         UpdateScore ();
     }
     
     void UpdateScore ()
     {
         scoreText.text = "Score: " + score;
     }
 }
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
avatar image
3

Answer by SetiFX · Jan 09, 2015 at 04:25 PM

These script modification worked for me too, but I had to remove the the same line

  **scoreText = GetComponent();** mentioned above by LarryWP

It would be great if the tutorial was updated using the UI

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
avatar image
1

Answer by awest · Oct 08, 2014 at 12:11 AM

The problem is that the GUIText variable will only accept GUIText. You need to change the variable type definition to be that of the text. That's just Text instead of GUIText.

 public Text scoreText;

This tutorial on [UI>Text][1] I thought wasn't very helpfull, but near the end is a tiny bit of code for reference. It looks like you may also need this in your Start function.

 scoreText = GetComponent<Text>();

Since this is brand new and only in the beta. It may take some finagling. [1]: http://unity3d.com/learn/tutorials/modules/beginner/ui/ui-text

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
avatar image
1

Answer by LarryWP · Dec 05, 2014 at 11:47 AM

Is your text built under the Canvas object? I did what you did above and I'm able to drop it in the GameController like the tutorial, but when I play, the text does not update and I get an error saying that the text is not set to an object. I'm stuck here now big time. I'm using the 4.6xx version I downloaded today 12-04-2014. I'm at my iPad and can't remember the exact version, but it's newer than your post above. All text now goes under a canvas and I wonder if that's fouling me up.

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 blatobran · Dec 29, 2014 at 10:42 AM 0
Share

try removing the line:

 scoreText = GetComponent<Text>();

it worked for me.

cheers

avatar image
0

Answer by PolarPanda · Mar 29, 2017 at 05:00 PM

I had to add this at the top of my GameController.cs script:

 using UnityEngine.UI;

and as suggested by awest, I changed made this change to scoreText:

 public Text scoreText;

I'm on Unity 5.6.

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

8 People are following this question.

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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Unity UFO tutorial question about code 0 Answers

Flip over an object (smooth transition) 3 Answers

learn c#? please help. 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