Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Only one object is being changed when ran

Discussion in 'Scripting' started by jamzam90, Jun 26, 2015.

  1. jamzam90

    jamzam90

    Joined:
    Jun 23, 2015
    Posts:
    6
    For some reason on one of the buttons that im creating is getting an image. The image however is not the right one either. What I'm trying to do is display a different image on each button that displays the races symbol.

    Code (CSharp):
    1. for (int i = 0; i <races.Count ; i++) {
    2.             Vector3 pos = new Vector3(0,0,0);
    3.  
    4.  
    5.             GameObject clone = Instantiate(Resources.Load ("Prefabs/Buttons/Race Button"), Vector3.zero, Quaternion.identity) as GameObject;
    6.             Button RaceB = clone.GetComponent<Button>();
    7.             Transform RaceImgGo = RaceB.transform.Find("RaceImage");
    8.             GameObject go = new GameObject();
    9.             go = GameObject.Find("RaceImage");
    10.             Image RaceImg = go.GetComponent<Image>();
    11.             Sprite RaceSprite = Resources.Load<Sprite>("Textures/RaceIcons/" + races[i]);
    12.             //RaceSprite = new Sprite();
    13.             Debug.Log("Sprite for " + races[i] + " " + RaceSprite.name  );
    14.             RaceImg.overrideSprite  = RaceSprite;
    15.             RaceImg.sprite  = RaceSprite;
    16.             //RaceImg.enabled = false;
    17.             //RaceB.onClick.AddListener(WorldCreationMenu);
    18.  
    19.             Text RaceText = RaceB.GetComponentInChildren<Text>();
    20.             RaceText.text = races[i];
    21.             RaceB.transform.parent = RaceSel.transform;
    22.             RaceB.transform.position = pos;
    23.  
    24.         }
     
  2. unitychristy2

    unitychristy2

    Joined:
    Sep 8, 2013
    Posts:
    38
    If you change line 1 from
    1. for (int i = 0; i <races.Count ; i++) {
    to

    for (int i = 1; i <races.Count ; i++) {

    do you get a different set of images?
     
  3. jamzam90

    jamzam90

    Joined:
    Jun 23, 2015
    Posts:
    6
    The iteration works fine it creates the buttons but only one image shows on the first button. Changing that removes a button and the image is on what is normally the second button.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    Code (csharp):
    1.  
    2. GameObject go = new GameObject();
    3. go = GameObject.Find("RaceImage");
    4.  
    what are you trying to do here? that second line will return the first "RaceImage" it finds in the scene every time, therefore you only update a single image using that "go" reference
     
    jamzam90 likes this.
  5. jamzam90

    jamzam90

    Joined:
    Jun 23, 2015
    Posts:
    6
    Im trying to have the image change per button.
     
  6. jamzam90

    jamzam90

    Joined:
    Jun 23, 2015
    Posts:
    6
    I've figured out how to get it to work now. Setting the name of "go" after i have found it works.
    Code (CSharp):
    1. GameObject go = new GameObject();
    2.             go = GameObject.Find("RaceImage");
    3.             go.name = "Image";
     
  7. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    or you could just remove lines 8 and 9 from the op and use your reference RaceImgGo where ever you use go... rather than having broken code and hacking at the naming... but ok :)
     
    jamzam90 likes this.
  8. jamzam90

    jamzam90

    Joined:
    Jun 23, 2015
    Posts:
    6
    At least i got it working. Then how would i use a transform as a object? Thats why im doing it that way so i can get the object and mess change the image.
     
  9. jamzam90

    jamzam90

    Joined:
    Jun 23, 2015
    Posts:
    6
    I didnt know a transform was able to be used the same way as a gameobject was.
     
  10. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    gameobjects are basically anything you see in the hierarchy, every one of them has a transform component which contains the xyz and rotation properties. You can access the gameobject from the transform, and the transform from the gameobject

    Code (csharp):
    1.  
    2. myGameObject.transform;
    3. myTransform.gameObject;
    4.  
    etc.