Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development How to Make a Video Game Score, Enemies, and Game State Script the Score Counter

Thale van der Sluijs
Thale van der Sluijs
1,756 Points

Flies wont move and die anymore

In the past videos everthing worked, the flies were moving and dying. But now I added the ScoreCounter script, the flies do nothing. This is my ScoreCounter code:

          <p>using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ScoreCounter : MonoBehaviour {

    public static int score;
    private Text scoreCounterText;

    // Use this for initialization
    void Start () {

        score = 0;
        scoreCounterText = GetComponent<Text> ();
    }

    // Update is called once per frame
    void Update () {

        scoreCounterText.text = score + " Flies";
    }
}
</p>
          ```

3 Answers

Thale van der Sluijs
Thale van der Sluijs
1,756 Points

And this is my FlyPikup code:

<p>
using UnityEngine;
using System.Collections;

public class FlyPickup : MonoBehaviour {

    [SerializeField]
    private GameObject pickupPrefab;


    void OnTriggerEnter(Collider other) {
        // if collider other is tagged with player ... excecute code
        if (other.CompareTag ("Player")){

            Instantiate(pickupPrefab, transform.position, Quaternion.identity);

            FlySpawner.totalFlies--;

            ScoreCounter.score++;

            Destroy (gameObject);
        }
    }
}

</p>
kevin kelly
kevin kelly
690 Points

Not an expert but maybe try closing the space between Destroy and (gameObject) so it is Destroy(gameObject); instead of Destroy (gameObject);

I could be wrong, but it is the only thing that I see is different then mine.

<p>
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class ScoreCounter : MonoBehaviour
{

    public static int score;
    private Text scoreCounterText;

    // Use this for initialization
    void Start()
    {
        score = 0;
        scoreCounterText = GetComponent<Text>();
    }

    // Update is called once per frame
    void Update()
    {
        scoreCounterText.text = score + " Flies";
    }
}
<p>
Cam Newton
Cam Newton
1,737 Points

I'd suggest looking at the fly script, as these shouldn't impact the fly's movement/triggers. If you're convinced they are affecting the flies, remove the script temporarily.

Check that your prefabs have the scripts attached and the Transforms are set. Sometimes a script component breaks and gets reset and you need to drag the prefabs back into the Inspector fields again.