Sry for posting it here.Seems that unity answers is not working.
I am changing the color of a gamobject through color.lerp. I have 1 script for changing the color. I am doing that when i click on a gameObject, it changes the color. But if i click on a gameobject and then click on a second gameobject while the color of the first object is still changing, the color of the first gameObject stops changing and the color of the first gameObject is now in between the 2 colors that i wanted.Imagine going from blue to red. I clicked on an object and then clicked on another object while the first one was still trasistioning, the second object changes but the first one is now pinkish color. It works correctly if i just use
if(gameObject.name==“object 1”)
{
// Change Color of object 1
}
if(gameObject.name==“Object 2”)
{
//Change Color of object 2 and so on
}
…
but I dont want to do it for every object. i want to do something like
int i = collider.gameObject.Get Something Which I Can Use As An Array Index
cube_.BlaBlaBla.Color = Color.lerp //Change color of cube where i is the array index of the gameObject_
I want to assign each gameObject a number so that i can reference each gameObject through an array index but i dont know how to do so. Can anyone please suggest me a solution to my problem.
Note:- If i simply just use gameObject.BlaBlaBla.Color = Color.lerp
it stops the changing of color of the previous gameObject
You can assign a number (or any variable) by way of a script/component
You could add it to a script that is already on each object, or make a new script entirely (whichever seems more suiting to you).
int i = collider.GetComponent<NumberForCube>().cubeNumber; // for instance
Okay, there’s another option of course… which may work for you. If your cubes are in an array, you could check the cube you’re clicking against its place in the array
1 Like
Yeah, any script on the object with a public int indexNum or something. You would also give these objects you want in an array a tag name.
It sounds like you are using a type of game manager, so could use:
GameObject[] objects;
void Start(){
objects = FindGameObjectsWithTag("tagname");
for(int i;i<objects.Length;i++){
objects[i].GetComponent<scriptName>().indexNum = i;
}
}
No idea why you would want to do that, so maybe that’s way off, but that’s how I would find an index from an individual.
1 Like
I tried doing it by adding 1 script to all the cubes and assigning a different integer value (starting form 0) to each and then did the GetComponent().variable before calling the color.Lerp. It changes the color but then it’s just the same story again. If i click the second object while the first one is changing , the change stops and the color is now in between. I am using a while loop for color change. So maybe the fault is in the function . The color.Lerp function isn’t mine. I copied it from the internet. This is my code
IEnumerator ColorChange()
{
arrayIndexNumber = gb.GetComponent().indexNum;
Debug.Log(arrayIndexNumber);
float progress = 0; //This float will serve as the 3rd parameter of the lerp function.
float increment = smoothness / duration; //The amount of change to apply.
//if (gb.name == “0”)
//{
if (currentColor == blue.color)
{
while (progress < 1)
{
cubes[arrayIndexNumber].GetComponent().material.color = Color.Lerp(currentColor, red.color, progress);
progress += increment;
yield return new WaitForSeconds(smoothness);
}
currentColor = gb.GetComponent().material.color;
}
if (gb.GetComponent().material.color == red.color)
{
while (progress < 1)
{
cubes[arrayIndexNumber].GetComponent().material.color = Color.Lerp(currentColor, blue.color, progress);
progress += increment;
yield return new WaitForSeconds(smoothness);
}
}
//}
//if (gb.name == “1”) and so on…
Okay, I’m a bit tired but I’m pretty sure that coroutine could be made a little nicer…
Is there just this 1 coroutine? It’s not on each cube, just 1 that you start when you click the cube?
That was just half for curiousity.
Could you try creating a variable at the top of the IEnumerator, something like arrayindex that you have, but a new one. Like : int localIndex = (same code you have now) and see if that fixes it?
1 Like
It occurs to me, if I see your intention now, and what you want is only to ensure that the lerping of the colour completes, then you don’t need an array or an index number or anything… it should be able to keep going until it completes, regardless. Perhaps I should have asked this earlier, but I wasn’t sure if you had another use for this index/number Sorry.
For instance, you could have sent the cube as a parameter to the coroutine…
1 Like
WOW! It works perfectly fine just by adding a local integer. I just added
int localIndex = arrayIndexNumber into the coroutine;
and switched the arrayIndexNumber to localIndex when referencing the array. If i now click in between another transition, the first one changes the color completely and not leaves it in between.
And your question that is there just 1 coroutine. Yes , there’s only this one.
Thanks a lot @methos5k and @fire7side for the help. I really appreciate it.
Cool … Glad you got it resolved