0

I have a script called Death which re spawns the player at the beginning location when the collision is true. I am trying to make a score count that when this collision is true it will minus 100 points but have been unsuccessful. The script bellow if from the score and death script. Any help would be much appreciated.

Score script:

    var gui : GameObject;
static var score : int;
Death.death = false;

function Start () 
{
    gui.GetComponent ("GUIText").text = "Score: 0";
}


function Update () 
{
    gui.GetComponent ("GUIText").text = "Score: " + score;
   if (death)
    {
        score = score - 100;
    }
}

Death Script:

#pragma strict 
var Ball : Transform;
public var death : boolean = false;

function OnCollisionEnter (b : Collision) 
{
 if (b.gameObject.tag == "Ball")
    {
     death = true;
     Ball.transform.position.x = 1.6;
     Ball.transform.position.y = 1.5;
     Ball.transform.position.z = 1.1;
     Ball.GetComponent.<Rigidbody>().velocity.y = 0;
     Ball.GetComponent.<Rigidbody>().velocity.x = 0;
     Ball.GetComponent.<Rigidbody>().velocity.z = 0;
    }
}
1
  • In the update function, put the if (death) check before setting the score. Commented May 26, 2016 at 1:43

2 Answers 2

1

I hope, that I can help you even though I'm using C#. It should be very easy to translate this to UnityScript.

using UnityEngine;

public class Score : MonoBehaviour
{
    public GUIText guiText;
    int score;

    void Update()
    {
        if(DeathTrigger.wasTriggered)
        {
            DeathTrigger.wasTriggered = false;
            score -= 100;
        }
        guiText.text = string.Format("Score: {0}", score);
    }
}

public class DeathTrigger : MonoBehaviour
{
    public static bool wasTriggered;

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.CompareTag("Ball"))
        {
            wasTriggered = true;
            // ...
        }
    }
}

I assume this is a beginner's questions, so I won't say anything about how static variables are evil and so on, but I still want to post an example of where to go next for a better approach:

using System;
using UnityEngine;
using UnityEngine.UI;

public class BetterDeathTrigger : MonoBehaviour
{
    // This event will be called when death is triggered.
    public static event Action wasTriggered;

    void OnCollisionEnter(Collision other)
    {
        if (other.gameObject.CompareTag("Ball"))
        {
            // Call the event.
            if (wasTriggered != null)
                wasTriggered();
            // ...
        }
    }
}

public class BetterScore : MonoBehaviour
{
    public Text uiText; // Unity 4.6 UI system
    int score;

    void Start()
    {
        // Subscribe to the event.
        BetterDeathTrigger.wasTriggered += WasTriggered_Handler;
    }

    // This method will now be called everytime the event is called from the DeathTrigger.
    private void WasTriggered_Handler()
    {
        score -= 100;
        uiText.text = string.Format("Score: {0}", score);
    }
}

A couple of things:

  • GUIText is pretty old and was already replaced by the new UI system since Unity version 4.6
  • Static variables are not smart in the long run, prefer instances of objects unless you are very sure how statics work
  • This is good example of where to use events. Again, it being static might lead to problems but for the first example it's the easiest.
  • The Unity Learn site offers a lot of tutorials about programming concepts such as "Communicating between scripts", they also have basic game examples where you can follow along with a complete project.
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. Hopefully this will dramatically improve my game.
0

It's definitely worth trying what Xarbrough suggested to go with instead. Statics can be confusing and get in the way in the long run. But here's how you can do it, written in Javascript.

public class Death { // you can change the class name to something that is broad enough to hold several public static variables
    public static var death : boolean;
}//This will end this class. When you make public classes like this, the script doesnt even need to be attached to a object, because it doesn't use Mono behavior

//This would be the actual DeathScript, whats above is technically not part of the Death script

var Ball : Transform;

function OnCollisionEnter (b : Collision) {
   if (b.gameObject.tag == "Ball"){
   Death.death = true;
   Ball.transform.position.x = 1.6;
   Ball.transform.position.y = 1.5;
   Ball.transform.position.z = 1.1;
   Ball.GetComponent.<Rigidbody>().velocity.y = 0;
   Ball.GetComponent.<Rigidbody>().velocity.x = 0;
   Ball.GetComponent.<Rigidbody>().velocity.z = 0;
 } }

From there, anytime you want to access the static variable, just tell it where to look. Death.death. Hope this helps!

3 Comments

Thanks again. Just wandering how exactly would i format this would i put it into one script and that is only the death script, how will i refer to that in the score script to say that when collision is true the gui text displays whatever the score is minus 100.
You can stick the public class Death in your death script if you want, or you can make a separate script and stick it on there. As for the gui stuff, handle how you had it, put it on the object you want the check to happen with. Just make sure to refer to the static variable with the class name. Also, if this answer helped, would you mind giving an upvote and marking it as the correct answer? :)
Oh, and make sure you are actually giving the score text the value you want, tell the text to equal "Score" + score;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.