Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Counting Set.Active State Changes On GameObjects

Discussion in 'Scripting' started by bc4042, Jun 10, 2020.

  1. bc4042

    bc4042

    Joined:
    Jun 10, 2020
    Posts:
    6
    New to C# trying to figure a logical approach to this problem. Any help would be greatly appreciated. I have 5 GameObjects, when clicked with MouseButtonDown they go from Set.Active(true) to Set.Active(false). This of course makes them disappear in the game. I'd like to count these when succesfully clicked by the mouse, and after the 5 objects are counted have that count of 5, activate a new animation state. Can't quite find a straight forward answer online.
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Do you have another script that is keeping track of the count? That's what I would do.
    When you click on the object. in the Onclick event have it run a function on the script that is keeping track of the count.
    I'n this function you can also check what the count is equal to. if == 5 then play animation.
    know you'll have to tell this script what object has the animation and the animation variable, bla bla bla. you might want to create a bool or something on the animation object. so when count ==5 you set the bool to true. just thinking of options for you.

    Do you know how to play animation?
     
  3. dahiyabunty1

    dahiyabunty1

    Joined:
    Jan 22, 2020
    Posts:
    68
    public static int count ;

    ondisable()
    {
    count++;
    }
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    this script looks to attached to the object you're disabling. you need to make sure that the static int is still accessible when all objects are disabled.

    Where is the script that sets the animation state? or is it reading the static int?

    i believe the function needs caps
    Code (CSharp):
    1. void OnDisable()
    2.     {
    3.         Debug.Log("PrintOnDisable: script was disabled");
    4.     }
     
  5. bc4042

    bc4042

    Joined:
    Jun 10, 2020
    Posts:
    6
    Sorry to you all. When I mean new I mean more like "copy and paste" new. I am still trying to figure out why on most everything.

    No I am not keeping count - Trying to figure that out. I thought I had some code (see 1 below) but it refers to Text and I don't need text on the screen and it did not work for counting.

    As far as switching from one animation to the next I was looking at this script (see 2 below).

    Feels like there should be a way of combining the two but that shows you how much I know :(

    Code 1 Keeping Score

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. public class PlayerControl : MonoBehaviour
    5. {
    6.     private int count;
    7.     public Text countText;
    8.     void Start()
    9.     {
    10.         count = 0;
    11.         SetCountText();
    12.     }
    13.     void OnMouseDown(Collider other)
    14.     {
    15.         if (other.gameObject.CompareTag("Lug"))
    16.         {
    17.             other.gameObject.SetActive(false);
    18.             count = count + 1;
    19.             SetCountText();
    20.         }
    21.         void SetCountText()
    22.         {
    23.             countText.text = "Count: " + count.ToString();
    24.         }
    25.     }
    26. }
    Code 2 Based on Score of 5 Que Animation

    Code (CSharp):
    1. private int score = 0;  //The current score
    2. private int scoreToTriggerTirePull = 5;  //The score which triggers animation
    3.  
    4. //Call this to add to the score
    5. public void AddToScore(int addToScore)
    6. {
    7.     score += addToScore;
    8. }
    9.  
    10. //Return the current score
    11. public int GetCurrentScore()
    12. {
    13.     return score;
    14. }
    15.  
    16. //Check if the score reaches the level which should trigger the animation
    17. private void Update()
    18. {
    19.     if (score >= scoreToTriggerTirePull)
    20.     {
    21.         startTirePull();
    22.     }
    23. }
    24.  
    25. //Call this to kick off animation "TirePull"
    26. private void startTirePull()
    27. {
    28.     //Add whatever code here to start TirePull animation
    29.     SceneManagement.SceneManager.LoadScene("TirePull");
    30. }
     
  6. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I removed the Text elements
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. public class PlayerControl : MonoBehaviour
    5. {
    6.     private int count;
    7.  
    8.     void Start()
    9.     {
    10.         count = 0;
    11.     }
    12.     void OnMouseDown(Collider other)
    13.     {
    14.         if (other.gameObject.CompareTag("Lug"))
    15.         {
    16.             other.gameObject.SetActive(false);
    17.             count = count + 1;
    18.         }
    19.     }
    20. }
     
  7. bc4042

    bc4042

    Joined:
    Jun 10, 2020
    Posts:
    6
    Thanks. I had a rare epiphany after I sent the code out, and did the exact, same but I cannot see where the count is being kept. It is not in the console.
     
  8. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    add [serializable] just above the count. This will display the private int in the inspector.
    1. public class PlayerControl : MonoBehaviour
    2. {
    3. [serializable]
    4. private int count;

    5. void Start()
    6. {
    7. count = 0;
    8. }
     
  9. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
  10. bc4042

    bc4042

    Joined:
    Jun 10, 2020
    Posts:
    6
    I had tried a public count field thinking if the code was working it would display on the script inspector. [SerializeField] does display on the Inspector also, but there's a disconnect somewhere because its not counting. The tagged "Lug" objects. It still remains at zero.

    Frustrating
     
  11. bc4042

    bc4042

    Joined:
    Jun 10, 2020
    Posts:
    6
    gonna have to do a raycast I guess, even more to study
     
  12. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    so, have you added the tag name "Lug" to the system? have you added the tag "Lug" to the objects you are looking for?
     
  13. bc4042

    bc4042

    Joined:
    Jun 10, 2020
    Posts:
    6
    Yes each of the five game objects has a "Lug" tag