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. Dismiss Notice

Question Deactivating Objects (Random..)

Discussion in 'Scripting' started by SmallLion, Mar 25, 2021.

  1. SmallLion

    SmallLion

    Joined:
    Oct 7, 2020
    Posts:
    20
    My problem is that, I have created a Random Generator script that Activate Random GameObjects but once that all gameobject activated, it doesn't work more. And I want to active only one GameObject once(Previously activated GameObject should be Deactivate.)So this is my code

    Code (CSharp):
    1. public class RandomColorGenerator : MonoBehaviour
    2. {
    3.     public GameObject Yellow, Red, Blue, Green;
    4.     public void Generator()
    5.     {
    6.         List  <GameObject> colors = new List<GameObject>();
    7.         colors.Add(Yellow);
    8.         colors.Add(Blue);
    9.         colors.Add(Red);
    10.         colors.Add(Green);
    11.  
    12.        colors[Random.Range(0, colors.Count)].SetActive(true);
    13.  
    14.     }
    15. }
    Please help! (sorry for bad English!)
     
  2. BastienKilosaurus

    BastienKilosaurus

    Joined:
    Aug 16, 2018
    Posts:
    8
    Try this
    Code (CSharp):
    1. public class RandomColorGenerator : MonoBehaviour
    2. {
    3.     public List  <GameObject> colors
    4.     GameObject picked = null;
    5.  
    6.     public void Generator()
    7.     {
    8.         picked?.SetActive(false);
    9.         picked = colors[Random.Range(0, colors.Count)];
    10.         picked.SetActive(true)
    11.     }
    12. }
     
    SmallLion likes this.
  3. SmallLion

    SmallLion

    Joined:
    Oct 7, 2020
    Posts:
    20
    Thank you very much! Saved My Life!;) Another problem. I want to activate a certain button, on every randomized card. I mean if the 'Red'
    Code (CSharp):
    1.  public void ActiveIfCardIsCorrect()
    2.     {  if(picked = colors[0])
    3.         {
    4.             RomeButton.SetActive(true);
    5.         }
    6.         if (picked = colors[1])
    7.         {
    8.           LondonButon.SetActive(true);
    9.         }
    10.         if (picked = colors[2])
    11.         {
    12.             TajmahalButton.SetActive(true);
    13.         }
    14.         if (picked = colors[3])
    15.         {
    16.             EiffelButton.SetActive(true);
    17.         }
    18.         if (picked = colors[4])
    19.         {
    20.             ChristButton.SetActive(true);
    21.         }
    22.  
    23.  
    24.     }
    card activated, Red button should be activated. I tried this but not working,
     
    Last edited: Mar 26, 2021
  4. BastienKilosaurus

    BastienKilosaurus

    Joined:
    Aug 16, 2018
    Posts:
    8
    Here in each of the "if" condition you are doing
    Code (CSharp):
    1. picked = colors[X]
    The problem is: "=" is an affectation operator, and you want to do a test, so it's "==" instead.
     
    SmallLion likes this.
  5. SmallLion

    SmallLion

    Joined:
    Oct 7, 2020
    Posts:
    20
    It works but not properly. When a Shape activated, it doesn't deactivate. Script here and Additionally I created a Button to Random Objects and Activate buttons according to the Activated object. It means both functions.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class RandomColrGenerator : MonoBehaviour
    6. {
    7.     public List<GameObject> colors;
    8.  
    9.     public GameObject RomeButton, LondonButon, TajmahalButton, EiffelButton, ChristButton;
    10.  
    11.     GameObject picked = null;
    12.  
    13.     public void Generator()
    14.     {
    15.         picked?.SetActive(false);
    16.         picked = colors[Random.Range(0, colors.Count)];
    17.         picked.SetActive(true);
    18.  
    19.        
    20.      
    21.  
    22.        
    23.  
    24.     }
    25.  
    26.     public void ActiveIfCardIsCorrect()
    27.  
    28.      
    29.     {
    30.        
    31.  
    32.         if(picked == colors[0])
    33.         {
    34.             RomeButton.SetActive(true);
    35.         }
    36.         if (picked == colors[1])
    37.         {
    38.           LondonButon.SetActive(true);
    39.         }
    40.         if (picked == colors[2])
    41.         {
    42.             TajmahalButton.SetActive(true);
    43.         }
    44.         if (picked == colors[3])
    45.         {
    46.             EiffelButton.SetActive(true);
    47.         }
    48.         if (picked == colors[4])
    49.         {
    50.             ChristButton.SetActive(true);
    51.         }
    52.  
    53.  
    54.     }
    55. }