A Tall Idiot
A Tall Idiot

Reputation: 1

Counter to victory in Unity

have been having a problem while working in Unity. I simply want to increase a score every time a collision is made, but my code just isn't helping. I know it sounds basic but I've been looking around in the site for quite some time now and I haven't found an answer yet. Here's the script:

I have already checked and the "VictoryScreen" is already made and built.

public class PaintScriptGreen : MonoBehaviour {

    public GameObject CylGreen;

    private int score = 0;

    private Vector3 tempPos;

    private Quaternion tempRot;

    private GameObject tempCyl;

    void Awake () 
    {

    }

    public void AddScore (int scoreValue)
    {
            score += scoreValue;
    }

    void OnTriggerEnter(Collider col)
    {
        if (col.gameObject.tag == "Cylinder" && col.gameObject.tag != "TreeHolder") 
        {
            tempPos = col.gameObject.transform.position;
            tempRot = col.gameObject.transform.rotation;
            Destroy (col.gameObject);
            tempCyl = Instantiate(CylGreen, tempPos, tempRot) as GameObject;
            AddScore (1);
            if (score >= 4)
            {
                Application.LoadLevel ("VictoryScreen");
            }
        }

        if ((col.gameObject.tag != "Player")&&(col.gameObject.tag != "PlayPart")) 
        {
                // destroy self bullet
                Destroy (this.gameObject);
        }

    }

Upvotes: 0

Views: 147

Answers (1)

BrettMStory
BrettMStory

Reputation: 91

    if ((col.gameObject.tag != "Player")&&(col.gameObject.tag != "PlayPart")) 
    {
            // destroy self bullet
            Destroy (this.gameObject);
    }

This is probably the culprit. You're destroying "this" anytime the above conditions aren't true. Is this script on the bullet? If so, you'll need to move the scoring mechanism to an object that isn't getting destroyed OR make it a static variable. I don't like static variables, but it is a possible solution.

Upvotes: 1

Related Questions