Search Unity

Instantiating Prefab with its colour set by an int, but its previous value is used?

Discussion in 'Getting Started' started by Shin_Toasty, Jul 2, 2018.

  1. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    This is hard to explain, but: my game has a special stage like a clay pigeon shoot. You take one shot at a time and score according to the colour of the enemy target hit. No problems here - but - I've scripted a little 'trophy shelf' so that as you hit the targets, it instantiates 40 pixel-sized versions (separate prefabs BTW) under your score.

    Code (CSharp):
    1.     void TrophyShelf ()
    2.     {  
    3.         GameObject trophyShelf = Instantiate (EnemyIcon, new Vector3 (tad, 0, 0), Quaternion.identity) as GameObject;
    4.         EnemyIcon.GetComponent<Image>().color = enemycolors [(newScoreValue)];
    5.         trophyShelf.transform.SetParent (trophyShelfParent.transform, false);
    6.         tad = tad + 50;
    7.     }
    Problem is: when you hit the first target a 'zero-coloured' trophy appears, when you hit the second one, the first one appears, third one = second one, and so on. I put a text field updated in Update in the game so I could watch a live view of the newScoreValue variable; it shows it is updating with the new score value immediately.

    The TrophyShelf () function is run after each shot - it runs 'on time'. All else works fine and no errors in the console.

    Is the answer printed on the end of my nose?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Yep. On line 4 you're changing the color of the prefab (EnemyIcon), not the color of the new instance (trophyShelf). :)
     
    Shin_Toasty likes this.
  3. Shin_Toasty

    Shin_Toasty

    Joined:
    Jun 15, 2017
    Posts:
    48
    Oh well yes, I mean, I knew that. The OP was a mis-paste, what I meant to ask was, what Time.deltaTime is it? I've got some lights baking and I don't want them to be overly compressed.

    I think if it hadn't instantiated anything I would have worked it out! Thanks again. You should get paid for this forum, Joe.;) I may have to hire you myself...
     
    JoeStrout likes this.