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
public class RandomColorGenerator : MonoBehaviour
{
public GameObject Yellow, Red, Blue, Green;
public void Generator()
{
List <GameObject> colors = new List<GameObject>();
colors.Add(Yellow);
colors.Add(Blue);
colors.Add(Red);
colors.Add(Green);
colors[Random.Range(0, colors.Count)].SetActive(true);
}
}
Please help! (sorry for bad English!)
Try this
public class RandomColorGenerator : MonoBehaviour
{
public List <GameObject> colors
GameObject picked = null;
public void Generator()
{
picked?.SetActive(false);
picked = colors[Random.Range(0, colors.Count)];
picked.SetActive(true)
}
}
1 Like
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’
public void ActiveIfCardIsCorrect()
{ if(picked = colors[0])
{
RomeButton.SetActive(true);
}
if (picked = colors[1])
{
LondonButon.SetActive(true);
}
if (picked = colors[2])
{
TajmahalButton.SetActive(true);
}
if (picked = colors[3])
{
EiffelButton.SetActive(true);
}
if (picked = colors[4])
{
ChristButton.SetActive(true);
}
}
card activated, Red button should be activated. I tried this but not working,
Here in each of the “if” condition you are doing
picked = colors[X]
The problem is: “=” is an affectation operator, and you want to do a test, so it’s “==” instead.
1 Like
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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RandomColrGenerator : MonoBehaviour
{
public List<GameObject> colors;
public GameObject RomeButton, LondonButon, TajmahalButton, EiffelButton, ChristButton;
GameObject picked = null;
public void Generator()
{
picked?.SetActive(false);
picked = colors[Random.Range(0, colors.Count)];
picked.SetActive(true);
}
public void ActiveIfCardIsCorrect()
{
if(picked == colors[0])
{
RomeButton.SetActive(true);
}
if (picked == colors[1])
{
LondonButon.SetActive(true);
}
if (picked == colors[2])
{
TajmahalButton.SetActive(true);
}
if (picked == colors[3])
{
EiffelButton.SetActive(true);
}
if (picked == colors[4])
{
ChristButton.SetActive(true);
}
}
}