Search Unity

Player wins when all game objects are collected

Discussion in 'Scripting' started by player3, Jan 11, 2014.

  1. player3

    player3

    Joined:
    Jan 11, 2014
    Posts:
    3
    I have a simple game where the player needs to collect 4 game objects within 30 sec. Now I already created the timer, so I need to let the game know that if all game objects are collected under the time limit the player wins.

    This is my code so far:

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameState : MonoBehaviour
    5. {
    6.     int count = 0;
    7.  
    8.     public float seconds = 30;
    9.     public float minutes = 0;
    10.  
    11.     // Use this for initialization
    12.     void Start ()
    13.     {
    14.    
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void Update ()
    19.     {
    20.         if (seconds <= 0)
    21.         {
    22.             seconds = 30;
    23.             if (minutes >= 1)
    24.             {
    25.                 minutes -- ;
    26.             }
    27.             else
    28.             {
    29.                 minutes = 0;
    30.                 seconds = 0;
    31.  
    32.                 GameObject.Find("TimerText").guiText.text = minutes.ToString("f0") + ":0" + seconds.ToString("f0");
    33.             }
    34.         }
    35.         else
    36.         {
    37.             seconds -= Time.deltaTime;
    38.         }
    39.  
    40.         if (Mathf.Round(seconds) <=9)
    41.         {
    42.             GameObject.Find("TimerText").guiText.text = minutes.ToString("f0") + ":0" + seconds.ToString("f0");
    43.         }
    44.         else
    45.         {
    46.             GameObject.Find("TimerText").guiText.text = minutes.ToString("f0") + ":" + seconds.ToString("f0");
    47.         }
    48.  
    49.         if(count >= 4) 
    50.         {
    51.             print("You Won!");
    52.         }
    53.     }
    54.  
    55.     void OnTriggerEnter(Collider collide)
    56.     {
    57.         if (collide.transform.tag == "Cube")
    58.         {
    59.             count = count + 1;
    60.             Destroy (collide.gameObject);
    61.         }
    62.         else if (collide.transform.tag == "Cube2")
    63.         {
    64.             count = count + 1;
    65.             Destroy (collide.gameObject);
    66.         }
    67.         else if (collide.transform.tag == "Cube3")
    68.         {
    69.             count = count + 1;
    70.             Destroy (collide.gameObject);
    71.         }
    72.         else if (collide.transform.tag == "Cube4")
    73.         {
    74.             count = count + 1;
    75.             Destroy (collide.gameObject);
    76.         }
    77.     }
    78. }
    Can anyone help me?
     
    Last edited: Jan 11, 2014
  2. player3

    player3

    Joined:
    Jan 11, 2014
    Posts:
    3
    Can anyone help me please? I have updated the CODE.
     
    Last edited: Jan 11, 2014
  3. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    Why are you tagging each cube with seperate tag?

    Are you sure you are not confusing name with tag?

    Try tagging all cubes as cube.

    Than change this:
    Code (csharp):
    1. void OnTriggerEnter(Collider collide)
    2. {
    3.     if (collide.transform.tag == "Cube")
    4.     {
    5.         count = count + 1;
    6.         Destroy (collide.gameObject);
    7.     }
    8.     else if (collide.transform.tag == "Cube2"
    9.     {
    10.         count = count + 1;
    11.         Destroy (collide.gameObject);      
    12.     }  
    13.     else if (collide.transform.tag == "Cube3")     
    14.     {      
    15.         count = count + 1;     
    16.         Destroy (collide.gameObject);      
    17.     }  
    18.     else if (collide.transform.tag == "Cube4")     
    19.     {
    20.         count = count + 1;     
    21.         Destroy (collide.gameObject);      
    22.     }  
    23. }
    to this:
    Code (csharp):
    1. void OnTriggerEnter(Collider collide)
    2. {
    3.     if(collide.gameObject.tag == "cube")
    4.     {
    5.         count++; // quick way of adding 1 to a variable
    6.         Destroy (collide.gameObject);
    7.     }
    8. }
     
    Last edited: Jan 11, 2014
  4. player3

    player3

    Joined:
    Jan 11, 2014
    Posts:
    3
    I have been working on it but now this is my problem. The Win part isn't working

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameState : MonoBehaviour
    5. {
    6.     int count = 0;
    7.     int maxItems = 4;
    8.  
    9.     public float seconds = 30;
    10.     public float minutes = 0;
    11.  
    12.     // Use this for initialization
    13.     void Start ()
    14.     {
    15.    
    16.     }
    17.    
    18.     // Update is called once per frame
    19.     void Update ()
    20.     {
    21.         if (seconds <= 0)
    22.         {
    23.             seconds = 30;
    24.             if (minutes >= 1)
    25.             {
    26.                 minutes -- ;
    27.             }
    28.             else
    29.             {
    30.                 minutes = 0;
    31.                 seconds = 0;
    32.  
    33.                 GameObject.Find("TimerText").guiText.text = minutes.ToString("f0") + ":0" + seconds.ToString("f0");
    34.             }
    35.         }
    36.         else
    37.         {
    38.             seconds -= Time.deltaTime;
    39.         }
    40.  
    41.         if (Mathf.Round(seconds) <=9)
    42.         {
    43.             GameObject.Find("TimerText").guiText.text = minutes.ToString("f0") + ":0" + seconds.ToString("f0");
    44.         }
    45.         else
    46.         {
    47.             GameObject.Find("TimerText").guiText.text = minutes.ToString("f0") + ":" + seconds.ToString("f0");
    48.         }
    49.  
    50.         if (count == maxItems  seconds < 30)
    51.         {
    52.             print("You Win!");
    53.         }
    54.         else if (count != maxItems  seconds == 0)
    55.         {
    56.             print("You Lose!");
    57.         }
    58.     }
    59.  
    60.     void OnTriggerEnter(Collider other)
    61.     {
    62.         if (other.gameObject.tag == "Cube")
    63.         {
    64.             other.gameObject.SetActive(false);
    65.             count = count + 1;
    66.         }
    67.         if (other.gameObject.tag == "Cube2")
    68.         {
    69.             other.gameObject.SetActive(false);
    70.             count = count + 1;
    71.         }
    72.         if (other.gameObject.tag == "Cube3")
    73.         {
    74.             other.gameObject.SetActive(false);
    75.             count = count + 1;
    76.         }
    77.         if (other.gameObject.tag == "Cube4")
    78.         {
    79.             other.gameObject.SetActive(false);
    80.             count = count + 1;
    81.         }
    82.     }
    83. }
     
  5. softwizz

    softwizz

    Joined:
    Mar 12, 2011
    Posts:
    793
    try putting public or private in front of the int on lines 6 and 7
     
  6. Infinite_Gamer

    Infinite_Gamer

    Joined:
    Dec 6, 2013
    Posts:
    11
    Make line 6 public