Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Can i assign a number to a gameobject?

Discussion in 'Scripting' started by abhaychandna, Nov 17, 2017.

  1. abhaychandna

    abhaychandna

    Joined:
    Aug 23, 2017
    Posts:
    7
    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
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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).
    Code (csharp):
    1.  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 :)
     
    abhaychandna likes this.
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    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:
    Code (csharp):
    1.  
    2. GameObject[] objects;
    3.  
    4. void Start(){
    5. objects = FindGameObjectsWithTag("tagname");
    6. for(int i;i<objects.Length;i++){
    7. objects[i].GetComponent<scriptName>().indexNum = i;
    8. }
    9. }
    10.  
    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.
     
    Last edited: Nov 17, 2017
    abhaychandna likes this.
  4. abhaychandna

    abhaychandna

    Joined:
    Aug 23, 2017
    Posts:
    7
    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<scriptname>().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<number>().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<Renderer>().material.color = Color.Lerp(currentColor, red.color, progress);
    progress += increment;
    yield return new WaitForSeconds(smoothness);
    }
    currentColor = gb.GetComponent<Renderer>().material.color;
    }

    if (gb.GetComponent<Renderer>().material.color == red.color)
    {
    while (progress < 1)
    {

    cubes[arrayIndexNumber].GetComponent<Renderer>().material.color = Color.Lerp(currentColor, blue.color, progress);
    progress += increment;
    yield return new WaitForSeconds(smoothness);
    }
    }
    //}

    //if (gb.name == "1") and so on....
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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? :)
     
    abhaychandna likes this.
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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...
     
    abhaychandna likes this.
  7. abhaychandna

    abhaychandna

    Joined:
    Aug 23, 2017
    Posts:
    7
    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.
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool .. Glad you got it resolved :)