cannot reduce score in my game

Hi guys,
I have a game where a bucket moves from left to right to catch the ball and in the top left it stores the score in a GUI label, when the player has caught ten it loads to scene two. What i am trying to do is when the player misses the ball each time the score reduces and lets say if it gets to five it loads to gameover here is my code for the box collider inside the bucket it is ticked as a trigger:

using UnityEngine;
using System.Collections;

public class EggCollider : MonoBehaviour {

    PlayerScript myPlayerScript;

    //Automatically run when a scene starts
    void Awake()
    {
        myPlayerScript = transform.parent.GetComponent<PlayerScript>();
    }

    //Triggered by Unity's Physics
	void OnTriggerEnter(Collider theCollision)
    {
        //In this game we don't need to check *what* we hit; it must be the eggs
        GameObject collisionGO = theCollision.gameObject;
        Destroy(collisionGO);

        myPlayerScript.theScore++;
    }	
	 
}

This is the playerscript where the score is rendered onto the screen each time the player catches the ball

using UnityEngine;
using System.Collections;

public class PlayerScript : MonoBehaviour {
    
    public int theScore = 0;  // defaulted to 0 at the start of the game

void Update () {
        //These two lines are all there is to the actual movement of the player 
        float moveInput = Input.GetAxis("Horizontal") * Time.deltaTime * 3; 
        transform.position += new Vector3(moveInput, 0, 0);

        //Restrict movement between two values from left to right on the x axis
        if (transform.position.x <= -3.0f || transform.position.x >= 3.0f)
        {
            float xPos = Mathf.Clamp(transform.position.x, -2.5f, 2.5f); //Clamp between min -2.5 and max 2.5
            transform.position = new Vector3(xPos, transform.position.y, transform.position.z);
        }
		
		if(theScore > 10)  // if you get 10 balls in the bucket 
		{
			Application.LoadLevel(2);   // load to scene 2
		}

   //OnGUI is called multiple times per frame. Use this for GUI stuff only!
    void OnGUI()
    {
        //We display the game GUI from the playerscript
        //It would be nicer to have a seperate script dedicated to the GUI though...
        GUILayout.Label("Score: " + theScore);
		GUILayout.Label("Welcome to CatchIt");   
    } 
	
}

I am trying to figure out how when i dont catch the ball or miss the call to decrement the score and then if i miss say five times it goes to the gameover. Do i have to use the OnCollisionEnter somehow where if the ball hit the floor i can reduce the score, any suggestions would be great

Kind regards :wink:

Put a script on the egg, which checks to see if it has missed the bucker or better move the whole detection to the egg.

Set the buckets GameObject tag to Bucket and attach this to your eggs

public class EggScript : MonoBehaviour {

 

    PlayerScript myPlayerScript;

 

    //Automatically run when a scene starts

    void Awake()

    {

        myPlayerScript = transform.parent.GetComponent<PlayerScript>();

    }

 

    //Triggered by Unity's Physics

    void OnTriggerEnter(Collider theCollision)

    {

        if (theCollision.gameObject.tag == "Bucket")
            {
                myPlayerScript.AddScore(1);
            }
        else
            {
                myPlayerScript.RemoveScore(1);
            }



        Destroy(gameObject);

    }   

     

}

and

public class PlayerScript : MonoBehaviour {

    

    public int theScore = 0;  // defaulted to 0 at the start of the game
    public int missStreak = 0;


    void Update()
    {

        //These two lines are all there is to the actual movement of the player 

        float moveInput = Input.GetAxis("Horizontal")*Time.deltaTime*3;

        transform.position += new Vector3(moveInput, 0, 0);



        //Restrict movement between two values from left to right on the x axis

        if (transform.position.x <= -3.0f || transform.position.x >= 3.0f)
            {

                float xPos = Mathf.Clamp(transform.position.x, -2.5f, 2.5f); //Clamp between min -2.5 and max 2.5

                transform.position = new Vector3(xPos, transform.position.y, transform.position.z);

            }

        if (missStreak >= 5)
            {
                //Do loosing here
            }

        if (theScore > 10) // if you get 10 balls in the bucket 
            {

                Application.LoadLevel(2); // load to scene 2

            }
    }

    public void AddScore(int amount)
    {
        missStreak = 0;
        theScore += amount;
    }

    public void RemoveScore(int amount)
    {
        missStreak++;
        theScore -= amount;
    }
 

   //OnGUI is called multiple times per frame. Use this for GUI stuff only!

    void OnGUI()

    {

        //We display the game GUI from the playerscript

        //It would be nicer to have a seperate script dedicated to the GUI though...

        GUILayout.Label("Score: " + theScore);

        GUILayout.Label("Welcome to CatchIt");   

    } 

    

}

Hi Elte thanks for that i applied the code i got no errors but now when i play the game if i catch the ball in the bucket its shows as minus -1 does not move and when another ball tries to go in it cant as the firstball is stuck as if its not destroying it here is my amended code, may i have gone wrong somewhere so all i am trying to do is if 5 balls past it goes to gameover, if i get 10 balls in loads to Scene 2 :wink: ps i will send some screenshots, thanks again for your help its much appreciated:

egg code

using UnityEngine;
using System.Collections;

public class EggCollider : MonoBehaviour {

    PlayerScript myPlayerScript;

    //Automatically run when a scene starts
    void Awake()
    {
        myPlayerScript = transform.parent.GetComponent<PlayerScript>();
    }

    //Triggered by Unity's Physics
	void OnTriggerEnter(Collider theCollision)
    {
		// ***** JUST TESTING **********************
		if(theCollision.gameObject.tag == "Bucket")
		{
			myPlayerScript.AddScore(1);
		}
		else
		{
			myPlayerScript.RemoveScore(1);	
		}

		
		
		Destroy(gameObject);
		// **** THIS BLOCK OF CODE *************************
		
        //In this game we don't need to check *what* we hit; it must be the eggs
        //GameObject collisionGO = theCollision.gameObject;
        //Destroy(collisionGO);

        //myPlayerScript.theScore++;
    }	
	 
}

PlayerScript code:

using System.Collections;

public class PlayerScript : MonoBehaviour {
    
    public int theScore = 0; 
	public int missStreak = 0;

	void Update () {
        //These two lines are all there is to the actual movement of the player 
        float moveInput = Input.GetAxis("Horizontal") * Time.deltaTime * 3; 
        transform.position += new Vector3(moveInput, 0, 0);

        //Restrict movement between two values from left to right on the x axis
        if (transform.position.x <= -3.0f || transform.position.x >= 3.0f)
        {
            float xPos = Mathf.Clamp(transform.position.x, -2.5f, 2.5f); //Clamp between min -2.5 and max 2.5
            transform.position = new Vector3(xPos, transform.position.y, transform.position.z);
        }
		
		// do losing here 
		if(missStreak >= 5)
		{
			// Load Game Over Screen
			Application.LoadLevel(3);
		}
		
		if(theScore > 10)  // if you get 10 balls in the bucket 
		{
			Application.LoadLevel(2);   // load to scene 2
		}
				
			
	}

	
	 public void AddScore (int amount)
	{
		
		missStreak = 0;
		theScore += amount;
		
	}
	
	public void RemoveScore(int amount)
	{
		
		missStreak++;
		theScore -= amount;
		
	}
	
	
	
	
    //OnGUI is called multiple times per frame. Use this for GUI stuff only!
    void OnGUI()
    {
        //We display the game GUI from the playerscript
        //It would be nicer to have a seperate script dedicated to the GUI though...
        GUILayout.Label("Score: " + theScore);
		GUILayout.Label("Welcome to CatchIt");   
    } 
	
}

Did you place the code on the egg and remove it from the bucket?

Hi Elite,

I dont understand what you mean i added the egg script and then you had another script for playerscript and i added that code that you put into it, what is it i have to remove from bucket in particular. Ok this is what i currently have:

Bucket - child object attached called egg collider - egg collider script where the add and remove score are
Player script is added to it aswell
Then there is egg script which just applies the force of the egg

The egg script i made goes on the egg not the bucket. The bucket should not have a script.

hi elite the bucket has the playerscript attached to it anyway it uses this to move the buckets from left to right so if i remove it it wont be able to move, so shoukld i put this on the egg collider or the egg prefab itself :slight_smile:

the player script stays on the bucket, the egg script i did goes on the actual egg object. Make sure the bucket or collider that catches the egg is tagged “Bucket”.

ok so on the egg prefab ok will try it out and let yo know, thanks elite:)