Edgar
Edgar

Reputation: 41

Referencing scoreManager when instantiating a player

I am wokring on a game where a player has to go through an enemy to score a point. But I am struggling to make the score count work. I am watching tutorials and trying various ways but something isnt working so I hope someone can help me here.

I have a player prefab, which gets instantiated when I press play in Unity. Then I have enemies spawning and when a player goes though one, the score should go to 1..and so on.

I have a gameObject in the scene with score script attached

public class Score : MonoBehaviour
{
    public Text scoreText;
    int score;
    // Start is called before the first frame update
    void Start()
    {
        score = 0;
    }

    public void ScoreUp()
    {
        score++;
        scoreText.text = score.ToString();
    }
}

Then I have a player script attached to a player prefab

public class PlayerController : MonoBehaviour
{
    private Score score; //Reference to Score script

    private void Start()
    {
        score = GameObject.FindWithTag("ReferenceManager").GetComponent<Score>();
    }

    private void Update()
    {
       if (Input.GetMouseButtonDown(0))
        {
            //movement code
        }
    }

    public void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Enemy"))
        {
            score.ScoreUp();
            Debug.Log("HIT");
        }
    }
}

Reference manager tag is the gameObject in the scene with score script. If someone could help me with this problem would be really awesome. Thank you

Upvotes: 0

Views: 221

Answers (2)

Edgar
Edgar

Reputation: 41

Apparently my score collider which is attached to enemy doesnt work.

I tried the same thing but with trigger collider on its own and the score count works perfectly. So I will play around and try to figure out the correct place for that trigger collider. Thank you whoever tried to help me! :)

UPDATE:

I had two scripts attached to scoreManager, and when i removed the other script, the score count started to work. Actually not sure why 2 scripts were conflicting but this fixed the problem.

Upvotes: 0

Define a gameobject variable so you can attach the score object to it in the editor

public class PlayerController : MonoBehaviour
{
    [SerializeField]
    public GameObject score_object;//This will allow u to select the player controller game object and set the variable to the score game object. The score game object is where the data will be stored.
    private Score score;

    private void Start()
    {
        score = score_object.GetComponent<Score>();
    }

    private void Update()
    {
       if (Input.GetMouseButtonDown(0))
        {
            //movement code
        }
    }

    public void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.CompareTag("Enemy"))
        {
            score.ScoreUp();
            Debug.Log("HIT");
        }
    }
}

I have never tried using the tag manager, What i suspect is that may be where your problem lies. Just try instancing it like this and see what happens.

Upvotes: 0

Related Questions