Search Unity

Variable not assigned

Discussion in 'Scripting' started by svhunt, Dec 22, 2014.

  1. svhunt

    svhunt

    Joined:
    Dec 22, 2014
    Posts:
    2
    So, I'm trying to do the Roll-A-Ball tutorial and I'm at section 7. I didn't find a better place to ask this so here goes. I'm trying to get the GUIText box to display properly, however, I keep getting UnassignedReferenceException: The variable countText of PlayerController has not been assigned. You probably need to assign the countText variable of the PlayerController script in the inspector. Now, I made sure to get my script as close to the video shows as possible and yet I still get this problem. Here's my code, obviously I'm doing something wrong but I don't know what. If anyone can help give pointers, that would be greatly appreciated.

    using UnityEngine;
    using System.Collections;

    public class PlayerController: MonoBehaviour
    {
    public float speed;
    public GUIText countText;
    private int count;

    void Start()
    {
    count = 0;
    SetCountText();
    }

    void FixedUpdate()
    {
    float moveHorizontal = Input.GetAxis("Horizontal");
    float moveVertical = Input.GetAxis("Vertical");

    Vector3 movement = new Vector3(moveHorizontal,0.0f,moveVertical);

    rigidbody.AddForce(movement * speed * Time.deltaTime);
    }

    void OnTriggerEnter(Collider other)
    {
    if(other.gameObject.tag == "PickUp")
    {
    other.gameObject.SetActive(false);
    count = count + 1;
    SetCountText ();
    }
    }

    void SetCountText()
    {
    countText.text = "Count: " + count.ToString();
    }
    }
     
  2. svhunt

    svhunt

    Joined:
    Dec 22, 2014
    Posts:
    2
    It would be nice if in the tutorial that they actually cover this step more clearly. It seems I had to search for CountText by clicking on the little circle thing to the right of Count Text and then clicking on CountText. That seemed to get it to work just fine now except that it goes 1-7, skips 8 and goes straight to 9 for count.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You can also drag and drop an item from the hierarchy or assets folder onto the inspector. Get used to dragging in things. Most scripts you write will require something to be dragged in.
     
    svhunt likes this.