0

I am learning to use Unity for my schoolwork, however, when I attempted to finish one of the tutorials, I came across a number of errors I was having with the tutorial, which appears to be outdated. My script is:

{

    public float speed;
    private Rigidbody rb;
    private int count; 
    public Text countText;

    void Start ()
    {
        rb = GetComponent<Rigidbody>();
        count = 0;
        SetCountText ();
    }

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

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

         rb.AddForce (movement * speed); 
    }

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

    void SetCountText ()
    {
        countText.text = "Count: " + count.ToString ();
        if (count >= 12)
        {
            winText.text = "You Win!";
        }
    }
}

The first error I was having was in the "void SetCountText ()" section. The compiler is telling me that the void keyword cannot be used in this context. What does that mean and how do I fix it?

The next error I am receiving is in the same location. I will paste it here for convenience. Assets/Scripts/PlayerController.cs(39,22): error CS1525: Unexpected symbol (', expecting,', ;', or=

I am also receiving another error, which I cannot explain beyond basic terms, and am unable to fix. The error is

NullReferenceException: Object reference not set to an instance of an object
PlayerController.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/PlayerController.cs:38)

Could I please recieve some information on how the script is errored and fixes for these errors? I know that all Unity tutorials use the "void" keyword when declaring a function, so could I please have some help with that?

Overall, I'm looking to understand what is going wrong and how to fix it.

Camilo Terevinto
  • 26,697
  • 6
  • 67
  • 99
Yayguy
  • 1
  • 3
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Camilo Terevinto Apr 02 '18 at 23:28
  • They're making you use Unity without first teaching you, or requiring the knowledge of, C#? That seems silly... – TJ Wolschon Apr 03 '18 at 12:14

1 Answers1

3

Looks like there's some missing parts to your code. And as far as your error, it looks like you're missing a closing brace } on your OnTriggerEnter() method. Try

{

public float speed;
private Rigidbody rb;
private int count; 
public Text countText;

void Start ()
{
    rb = GetComponent<Rigidbody>();
    count = 0;
    SetCountText ();
}

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

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

     rb.AddForce (movement * speed); 
}

void OnTriggerEnter(Collider other) 
{
    if (other.gameObject.CompareTag("Pickup"))
    {
        other.gameObject.SetActive(false);
        count = count + 1;
        SetCountText ();
    }
}
void SetCountText ()
{
    countText.text = "Count: " + count.ToString ();
    if (count >= 12)
    {
        winText.text = "You Win!";
    }
}
}
metaldino21
  • 308
  • 1
  • 7