Search Unity

access gameobjects

Discussion in 'Scripting' started by Deleted User, Jun 15, 2018.

  1. Deleted User

    Deleted User

    Guest

    i would like to learn this :
    i have a script attached to a button which has a public array field.

    by drag and drop from hierarchy, i assigned 9 gameobjects to this array field in the inspector .

    now i need to click on this button and access this array gameobjects component called "Fill Amount" and change that to a new value.

    though, in the script , i could not write the correct code.
    any idea ?
    thanks.
    Code (CSharp):
    1.  
    2. public class Boosttimes : MonoBehaviour
    3. {
    4.     private float nums = 1;
    5.     public GameObject[] AllObjstimes;
    6.  
    7.     public void Start()
    8.     {
    9.        
    10.     }
    11.  
    12.     public void FullFillAmounts()
    13.     {
    14.         Debug.Log ("refill items time!");
    15.  
    16.  
    17.     }
    18. }
    19.  
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Do you have UI button in the scene? If not you’ll need one.
    The ui button has an OnClick listener. And in the onclick listener you point it to the FullFillAmounts function
     
  3. Deleted User

    Deleted User

    Guest

    i have already done that. what i need actually is how to access those draged gameobjects component called Fill Amount.
    maybe i asked my question not clearly enough!
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I don’t see FillAmount.
    So I need some info about the other script.
     
  5. Deleted User

    Deleted User

    Guest

    UI Image has a property called Fill Amount . this property is needed to be referenced . you can learn about it in unity references.
    so in this script i need to reference it .
     
  6. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    ok, know i understand.
    So when you press the button. you want to change the UI gameobject fillamount to a new value.
    I'll slap some code together for you to look at and study!
     
  7. Deleted User

    Deleted User

    Guest

    nice!
    i searched alot , but it is not directly addressed how to be implemented.
    assigning a gameobject to a public field is easy , but how can you after that , access its components?
    this is what i needed to learn more.anyway thanks,
     
  8. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    here is a simple script. it will change the fill about to a different % every time you click the button.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class Boosttimes : MonoBehaviour
    6. {
    7.     private float nums = 1;
    8.     public GameObject[] AllObjstimes;
    9.     public Image imageWithAutofill;
    10.  
    11.     void Start()
    12.     {
    13.  
    14.     }
    15.  
    16.     public void FullFillAmounts()
    17.     {
    18.         Debug.Log("refill items time!");
    19.         float randomFill = Random.Range(0,1);
    20.         Debug.Log("Fill amount is " + randomFill);
    21.         imageWithAutofill.fillAmount = randomFill; //1 is %100 filled
    22.     }
    23. }
     
  9. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    From your last post. I'm thinking you want to change the fill amount on every GameObject that's in the array. Is this correct?
     
  10. Deleted User

    Deleted User

    Guest

    yes
     
  11. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    ok, i'll update the code for the array
     
    Deleted User likes this.
  12. Deleted User

    Deleted User

    Guest

    but i dont need a public image there , i need to address that image from code. i have already an array that reference to those gameobjects which each one has ui image in it.
    in void start() i need to access them through some syntax like findby tag or . . this is what i am looking to lean^^
     
  13. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    lets see if this is what you're after

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. class Boosttimes : MonoBehaviour
    6. {
    7.     private float nums = 1;
    8.     public GameObject[] AllObjstimes;
    9.  
    10.     void Start()
    11.     {
    12.  
    13.     }
    14.  
    15.     public void FullFillAmounts()
    16.     {
    17.         Debug.Log("refill items time!");
    18.  
    19.         //this is a loop that looks at evey GameObject in the array
    20.         for (int i = 0; i < AllObjstimes.Length; i++)
    21.         {
    22.             //now try and find all images that are subobjects from the main object.
    23.             Image[] childImages = AllObjstimes[i].GetComponentsInChildren<Image>();
    24.             if (childImages.Length > 0)
    25.             {
    26.                 //we found more then 1 Image gameObject, lets change each ones fill amount
    27.                 for (int k = 0; k < childImages.Length; k++)
    28.                 {
    29.                     float randomFill = Random.Range(0, 1);
    30.                     childImages[k].fillAmount = randomFill;
    31.                 }
    32.             }
    33.         }
    34.     }
    35. }
     
    Deleted User likes this.
  14. Deleted User

    Deleted User

    Guest

    wow thank i try it
     
  15. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    if the script doesn't work, can you post a screen shot of the UI hierarchy, so I can understand the setup a bit better.
     
  16. Deleted User

    Deleted User

    Guest

    so first thanks for your helping .this is not working correctly . i need to give you more information .
    this array gameobjects have a child . in this child , is fill amount .
    now no errors in console . however the image from child of array gameobject need to be accesses.
    can you change the code please?^^
     
  17. Deleted User

    Deleted User

    Guest

    of course!
     
  18. Deleted User

    Deleted User

    Guest

  19. Deleted User

    Deleted User

    Guest

    the idea is to get some coins from player and refill their items timer to maximum .
    i mean when you start the game , items from array start to count down .
    but this button will take some coins from player and give to their items again maximum time.
     
  20. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Looks like you have some compiler errors. It's helpful if you post those and the line(s) of code to which they refer when you're discussing an issue in the thread. :)

    The code provided looks good. Though, I don't think you need to check if it's greater than zero before looping, since the loop would do that anyways, but that doesn't matter. :)
     
    Deleted User likes this.
  21. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    ok, I see two things.
    you have an error on line 25. I don't get this error in my unity.
    second, it may be the source the error. "Image BG" looks to be disabled. it's grayed out.
     
  22. Deleted User

    Deleted User

    Guest

    hello sir.
    this code does not change fill amount to a new value . what is wrong? i fixed those errors .
     
  23. Deleted User

    Deleted User

    Guest

  24. Deleted User

    Deleted User

    Guest


    they are fixed.
    https://imgur.com/a/pSXxCHm

    you know , those image are located in array member as child. that is why code does not correctly . how will you though reference that child instead of array member ?
    how will you access array member child fill amount? ^^
     
  25. Deleted User

    Deleted User

    Guest

    it will be active as soon as each item from array is thrown to the yard.
    i need as soon as item is thrown away bg be active and boost time works!
     
  26. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    in my scene the fill is getting set to 0, so I'm gunna keep working on the script
     
  27. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    found my mistake,
    forgot the f for the float

    Code (CSharp):
    1.  
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. class Boosttimes : MonoBehaviour
    7. {
    8.     private float nums = 1;
    9.     public GameObject[] AllObjstimes;
    10.  
    11.     void Start()
    12.     {
    13.  
    14.     }
    15.  
    16.     public void FullFillAmounts()
    17.     {
    18.         Debug.Log("refill items time!");
    19.  
    20.         //this is a loop that looks at evey GameObject in the array
    21.         for (int i = 0; i < AllObjstimes.Length; i++)
    22.         {
    23.             Debug.Log("AllObjstimes " + i + " " + AllObjstimes[i].name);
    24.             //now try and find all images that are subobjects from the main object.
    25.             Image[] childImages = AllObjstimes[i].GetComponentsInChildren<Image>();
    26.             Debug.Log("found  " + childImages.Length + " child Objects");
    27.  
    28.             if (childImages.Length > 0)
    29.             {
    30.                 //we found more then 1 Image gameObject, lets change each ones fill amount
    31.                 for (int k = 0; k < childImages.Length; k++)
    32.                 {
    33.                     Debug.Log("childImages " + k + " " + childImages[k].name);
    34.                     float randomFill = Random.Range(0, 1f);
    35.                     Debug.Log("change fill to " + randomFill);
    36.                     childImages[k].fillAmount = randomFill;
    37.                 }
    38.             }
    39.         }
    40.     }
    41. }
    42.  
     
  28. Deleted User

    Deleted User

    Guest

    let me see!
     
  29. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    here is my scene Capture_Start.PNG Capture_AfterClick.PNG
     
  30. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    i forgot to clip the most important thing, the fill amount
    Capture_AfterClick.PNG
     
    Deleted User likes this.
  31. Deleted User

    Deleted User

    Guest

    what is attached to your ui button?
     
  32. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    my gameobject called scriptholder, scriptholder has boosttimes set in the OnClick event
     
  33. Deleted User

    Deleted User

    Guest

    for me , in the console it is changed to new value, but each item image fill amount does not get updated!
    what can be wrong ? i think my array is not addressed correctly
     
  34. Deleted User

    Deleted User

    Guest

    console shows item names . but fill amount is in the item child .how will you access child of those items instead of them?
    i mean one level deeper , and access children ?
     
  35. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    can you screen shoot me the debug output. I want to see how many children it finds. Mine finds 1
    found 1 child Objects
     
  36. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Line 24
    Image[] childImages = AllObjstimes.GetComponentsInChildren<Image>();

    this looks at all children, even grandchildren and great grandchildren.
     
  37. Deleted User

    Deleted User

    Guest

    https://imgur.com/a/8WqxMo0

    when i click on boost button , the button which run that method , in console fill amount is 1 but in action , each item child image fill amount dont get 1 .
    where is problem?
     
  38. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    Do you have any other scripts that are changing the fill amount?
    you said yesterday that the fill amount slowly depletes. I can only assume that the other script is keeping it 0.

    Can you post that script?
     
  39. Deleted User

    Deleted User

    Guest

    Code (CSharp):
    1. public bool isUsed;
    2.     private bool EnableFill;
    3.     public bool CanBeUsed;
    4.     public float countertimes;
    5.     private float FillAmount;
    6.     Animator anim;
    7.     private Transform Chield;
    8.  
    9.     void Start()
    10.     {
    11.         CanBeUsed = false;
    12.         isUsed = false;
    13.         FillAmount = 1f;
    14.         anim = GetComponent<Animator> ();
    15.         Chield = transform.GetChild (0);
    16.         Chield.gameObject.SetActive (false);
    17.     }
    18.  
    19.     void Update()
    20.     {
    21.         if (EnableFill)
    22.         {
    23.             FillAmount -= Time.deltaTime / countertimes;
    24.             Chield.transform.GetChild(0).GetComponent<Image> ().fillAmount = FillAmount;
    25.  
    26.             if (FillAmount <= 0)
    27.             {
    28.                 FillAmount = 0;
    29.                 GameController c = GameObject.FindObjectOfType<GameController> ();
    30.                 c.lives--;
    31.                 c.livesText.text = c.lives.ToString();
    32.                 c.livesimage.gameObject.GetComponent<Animator>().SetBool("ToLivesIcon",true);
    33.                 anim.SetBool ("returning", true);
    34.                 anim.SetBool ("Smashing", false);
    35.                 isUsed = false;
    36.                 ShowBar (false);
    37.                 Invoke ("ResestHeart", 1f);
    38.                 CanBeUsed = false;
    39.             }
    40.         }
    41.         else
    42.             FillAmount = 1f;
    43.     }
    44.  
    45.     public void ChangeState(bool state)
    46.     {
    47.         anim.SetBool ("Smashing",state);
    48.         anim.SetBool ("returning", false);
    49.     }
    50.  
    51.     public void ShowBar(bool isVisible)
    52.     {
    53.         Chield.gameObject.SetActive (isVisible);
    54.         EnableFill = isVisible;
    55.         CanBeUsed = true;
    56.     }
    57.  
    58.     public void Test()
    59.     {
    60.         ShowBar (true);
    61.     }
    62.  
    63.     void ResestHeart()
    64.     {
    65.         GameController c = GameObject.FindObjectOfType<GameController> ();
    66.         c.livesimage.gameObject.GetComponent<Animator>().SetBool("ToLivesIcon",false);
    67.     }
    this is attached to each of those item .
    one question on top of this :
    you start the game level . you need after let say 2 seconds , 1 item get thrown away . next 2 seconds 2 items get thrown ...next 2 seconds ...again 3 items get thrown .
    how will you do it with code?
    for this i have an invoke :
    void RandomTimer()
    {
    Invoke ("RandomItem", timeintervals);
    }

    but i dont know how to define items for that to be completed?
     
  40. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    What you should do is instead of keeping track of the fill amount on your own, is read back the current fill amount before adjusting in that script. I think that will fix your issue.

    Currently, that script may have altered the fill amount variable (for that script, not the image) to 0, then you try to set it to full, and this script thinks it wants to use zero.
     
  41. Deleted User

    Deleted User

    Guest

    sorry , i could not understand you at all.
     
  42. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Hm, sorry.. so, I think this snippet:
    Code (csharp):
    1. if (EnableFill)
    2. {
    3.     float fillAmount = Chield.transform.GetChild(0).GetComponent<Image>().fillAmount;
    4.     fillAmount -= Time.deltaTime / countertimes;
    5.     Chield.transform.GetChild(0).GetComponent<Image> ().fillAmount = fillAmount;
    6.  
    That's what I meant. You do not need to track the fill yourself. Just look it up. I would suggest that you cache that Image variable, of course.
     
  43. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    @codeandscripts I've been looking at the code and screen shots you've provided.
    I need to get a few thing confirmed
    I see 3 scripts, Boosttimes, GameController and anotherScriptNameUnknown is your last posted code, you say "this is attached to each of those item"

    where do the scripts live?
    UI instructor
    Canvas > Time Boost, has the Boosttimer script

    Canvas > Time Boost > Item01 Vase right > Image BG > image BG slider
    which one of these has the anotherScriptNameUnknown script on it? I assume it's "Item01 Vase right"

    If this is correct i'll see if i can get you a script to work for your setup.
     
  44. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    possible fix

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. class Boosttimes : MonoBehaviour
    6. {
    7.     private float nums = 1;
    8.     public GameObject[] AllObjstimes;
    9.  
    10.     void Start()
    11.     {
    12.  
    13.     }
    14.  
    15.     public void FullFillAmounts()
    16.     {
    17.         Debug.Log("refill items time!");
    18.  
    19.         //this is a loop that looks at evey GameObject in the array
    20.         for (int i = 0; i < AllObjstimes.Length; i++)
    21.         {
    22.             Debug.Log("AllObjstimes " + i + " " + AllObjstimes[i].name);
    23.             //now try and find all images that are subobjects from the main object.
    24.             Image[] childImages = AllObjstimes[i].GetComponentsInChildren<Image>();
    25.             Debug.Log("found  " + childImages.Length + " child Objects");
    26.  
    27.             if (childImages.Length > 0)
    28.             {
    29.                 //we found more then 1 Image gameObject, lets change each ones fill amount
    30.                 for (int k = 0; k < childImages.Length; k++)
    31.                 {
    32.                     if (childImages[k].type == Image.Type.Filled)// this just makes sure that the image is set to Fill
    33.                     {
    34.                         Debug.Log("I found a fill image: " + childImages[k].gameObject.name);
    35.                         FindObjectOfType<OtherScript>().ResetFillAmount(1f); //if you have more then 1 OtherScript in the scene this may not work as intended.
    36.                         //if it's a child object then this can be changed
    37.                     }
    38.                 }
    39.             }
    40.         }
    41.     }
    42. }
    and the otherScript
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class OtherScript : MonoBehaviour {
    7.  
    8.     public bool isUsed;
    9.     private bool EnableFill;
    10.     public bool CanBeUsed;
    11.     public float countertimes;
    12.     private float FillAmount;
    13.     Animator anim;
    14.     private Transform Chield;
    15.  
    16.     void Start()
    17.     {
    18.         CanBeUsed = false;
    19.         isUsed = false;
    20.         FillAmount = 1f;
    21.         anim = GetComponent<Animator>();
    22.         Chield = transform.GetChild(0);
    23.         Chield.gameObject.SetActive(false);
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         if (EnableFill)
    29.         {
    30.             FillAmount -= Time.deltaTime / countertimes;
    31.             Chield.transform.GetChild(0).GetComponent<Image>().fillAmount = FillAmount;
    32.  
    33.             if (FillAmount <= 0)
    34.             {
    35.                 FillAmount = 0;
    36.                 GameController c = GameObject.FindObjectOfType<GameController>();
    37.                 c.lives--;
    38.                 c.livesText.text = c.lives.ToString();
    39.                 c.livesimage.gameObject.GetComponent<Animator>().SetBool("ToLivesIcon", true);
    40.                 anim.SetBool("returning", true);
    41.                 anim.SetBool("Smashing", false);
    42.                 isUsed = false;
    43.                 ShowBar(false);
    44.                 Invoke("ResestHeart", 1f);
    45.                 CanBeUsed = false;
    46.             }
    47.         }
    48.         else
    49.             FillAmount = 1f;
    50.     }
    51.  
    52.     public void ChangeState(bool state)
    53.     {
    54.         anim.SetBool("Smashing", state);
    55.         anim.SetBool("returning", false);
    56.     }
    57.  
    58.     public void ShowBar(bool isVisible)
    59.     {
    60.         Chield.gameObject.SetActive(isVisible);
    61.         EnableFill = isVisible;
    62.         CanBeUsed = true;
    63.     }
    64.  
    65.     public void Test()
    66.     {
    67.         ShowBar(true);
    68.     }
    69.  
    70.     void ResestHeart()
    71.     {
    72.         GameController c = GameObject.FindObjectOfType<GameController>();
    73.         c.livesimage.gameObject.GetComponent<Animator>().SetBool("ToLivesIcon", false);
    74.     }
    75.  
    76. //this was added
    77.     public void ResetFillAmount(float setTo)
    78.     {
    79.         FillAmount = setTo;
    80.     }
    81. }
    82.  
     
  45. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    You also had a question about defining an Item. You will most likely need a class to define them, i would call the class Item. You need to think about what an Item is?
    Does an Item have health? does it have Hit points? does it cause damage? Is it usable, and that could mean many things. Do you need to pickup items? any other options you need can think of.
    You build a class that defines any of these values.
    Then in the main gameManager script. You can great a List<Item>. This stores any item in the world. and then you have a second script attached to the player to store the Items that he has picked up.