Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Weird game object/button issue

Discussion in 'Editor & General Support' started by Sobrtonog, Oct 15, 2019.

  1. Sobrtonog

    Sobrtonog

    Joined:
    Sep 28, 2019
    Posts:
    15
    Code (CSharp):
    1. GameObject a;
    2. void Func(){
    3.     GameObject temp = transform.Find("ButtonName").gameObject;
    4.     Destroy (temp); // This works, button disappears
    5.     a = temp; // This line throws an error
    6. }
    I'm trying to retrieve the button from child and add an onClick event, but every time I try to store the result from Find it throws an error.

    Changing 'a' and 'temp' to types of Button and using gameObject.GetComponent<Button>(); doesn't work either...
     
  2. Domas_L

    Domas_L

    Unity Technologies

    Joined:
    Nov 27, 2018
    Posts:
    111
    Hello! It seems that you're trying to use the "temp" object after destroying it - this shouldn't be done. Switching the lines 4 and 5 should fix these errors, since then you'd store the value of "temp" before it is destroyed