One script with multiple Gameobject only one Works
Hello, I have a problem with my code, What im trying to do is first activate randomly one gameobject of the array. then the gameobject do lerp emission. Then the player must select the correct one. If is incorrect the gameobject chosen will change to red, and lose. Works perfect with only one array. But when I try to do it with different arrays with that same code only work the first array and no the others one, the others gameobjects recognize as the incorrect one even if is the correct one. I m begginer programming, sorry if i didnt explain it well.
Here is my code:
public class Random_Paiting : MonoBehaviour
{
public GameObject[] CubesColumn;
public Material material;
GameObject current;
public float velocity;
bool activated = false;
int index;
public void Start()
{
index = Random.Range(0, CubesColumn.Length);
current = CubesColumn[index];
}
public void Update()
{
ClickMouse();
if (activated == false)
{
current.GetComponent
<MeshRenderer>().material.SetColor("_EmissionColor", Color.white * Mathf.LinearToGammaSpace(Mathf.Lerp(1, 0, Time.time * velocity)));
}
}
public void ClickMouse()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit,100))
{
if (hit.transform.gameObject == current)
{
activated = true;
current.GetComponent
<MeshRenderer>().material.SetColor("_EmissionColor", Color.white * Mathf.LinearToGammaSpace(10));
}
else
{
hit.transform.gameObject.GetComponent
<MeshRenderer>().material.SetColor("_EmissionColor", Color.red * Mathf.LinearToGammaSpace(10));
}
}
}
}
}
Your answer
Follow this Question
Related Questions
How do I access an object script variable that is in a 2D array? 0 Answers
NullReferenceException: Object reference not set to an instance of an object 0 Answers
Creating a grid that holds objects of different sizes? 0 Answers
GameObject changes to None in inspector after pressing Play 0 Answers
NullReferenceException: Object reference not set to an instance of an object error? 0 Answers