Search Unity

gameObject.SetActive not working?

Discussion in 'Scripting' started by Seppi, Nov 11, 2017.

Thread Status:
Not open for further replies.
  1. Seppi

    Seppi

    Joined:
    Nov 10, 2012
    Posts:
    162
    Here's my code. Clicking does nothing. Any ideas?

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Flash : MonoBehaviour {
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.        
    10.         gameObject.SetActive(false);
    11.     }
    12.    
    13.     // Update is called once per frame
    14.     void Update () {
    15.        
    16.         if(Input.GetButtonDown("Fire1")){
    17.             gameObject.SetActive(true);
    18.         }
    19.  
    20.         if(Input.GetButtonUp("Fire1")){
    21.             gameObject.SetActive(false);
    22.         }
    23.     }
    24. }
    25.  
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    Once it's inactive, it stops running the code to turn itself back on.
     
  3. JoeFernaldSG

    JoeFernaldSG

    Joined:
    Apr 3, 2017
    Posts:
    9
    A MonoBehaviors update function will only ever be called if the GameObject it is attached to is active. You disable your game object on Start(), so it's never active. It will never get an Update() call thus it won't ever call SetActive to true.

    There are 2 solutions to the problem, one is that you can enable/disable the actual monobehaviour using the GetComponent<Flash>().enabled = true (or false) or you can move this monobehaviour to another game object that is always enabled, which has a reference to the game object that you want to enable/disable.
     
    Bunny83, adehm and tonialatalo like this.
  4. Ex6tra

    Ex6tra

    Joined:
    Oct 31, 2019
    Posts:
    14
    I had this problem for so long and somehow changing the object name fixed it.
     
    Ensortainment and ddavebbell like this.
  5. ddavebbell

    ddavebbell

    Joined:
    May 24, 2020
    Posts:
    1
    Had the exact same problem, and this fixed it as well.
     
    Red_Wolf_Games and Ex6tra like this.
  6. weejiafoong

    weejiafoong

    Joined:
    Jan 21, 2018
    Posts:
    1
    same with me
     
  7. AshwinTheGammer

    AshwinTheGammer

    Joined:
    Sep 23, 2016
    Posts:
    69
    How did you do that tho?
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Don't reply to a three year old script with a one-sentence zero-content and zero-context post.

    Start your own post. It's FREE. Make sure you include ALL of the information necessary for someone to understand you.

    When you do, here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    If you post code, please use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    stain2319 and Bunny83 like this.
  9. DranKof

    DranKof

    Joined:
    Feb 15, 2019
    Posts:
    1
    Renaming the object worked for me too, this is a super useful thread.

    @Kurt-Dekker
    If people are still coming back to the thread it doesn't matter how old it is, it's still relevant. His question was a direct and logical follow-up to the previous statement and it required minimal further explanation. To make a new post "how to rename an object specifically to fix it when setActive fails" would actually be met with more confusion and require much more explanation than asking it here.

    @AshwinTheGammer
    Just rename it in any way. The easiest way to rename an object is in the Hierarchy (tree) view, left-clicking on the object and hitting F2. So if your GUI object that you're activating/deactivating is called "Dialog Box" you can rename it "Dialogue Box" (or anything else).
     
  10. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Please don't make this personal. It's a shame your first post was to criticise a long-time and very helpful member of the community TBH.
     
    stain2319, Bunny83 and Kurt-Dekker like this.
  11. stain2319

    stain2319

    Joined:
    Mar 2, 2020
    Posts:
    417
    @Kurt-Dekker has helped me (not only personally by responding to my posts, but by simply myself reading his posts in the threads of others) more than any single user on this forum, period. He is invited to all my future parties.
     
    cerber_rus, Bunny83 and Kurt-Dekker like this.
  12. Deleted User

    Deleted User

    Guest

    Ok I know I'm late and I'm not a very good programmer, but you might want to add; public GameObject gameObject; at the top.
     
  13. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,455
    Sorry but that makes no sense. Adding in a field for "gameObject" doesn't solved anything. A component already has a property named "gameObject" that retrieves the GameObject the component is on. You'd easily find this in the API docs here.
     
    AshwinTheGammer likes this.
  14. Marou1

    Marou1

    Joined:
    Dec 13, 2021
    Posts:
    161
    I have the same issue. And it is a very basic setup. I have a sphere (always active) and a cube.

    Case A: Sphere active, Cube active:
    From the sphere, Start, set the cube to inactive. It works.

    Case B: Sphere active, Cube inactive:
    From the sphere, Start, set the cube to active. It does not work. I get this error: InvalidOperationException: Missing target object for 'UnityEngine.GameObject.SetActive'.
     

    Attached Files:

    • 2.png
      2.png
      File size:
      14.9 KB
      Views:
      323
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    You may think so but this is one of the reasons that you should NOT reply to old threads.

    Here we are in the Scripting group and your post is clearly NOT appropriate for scripting. Your question involves the whole visual scripting package, which has its own forum:

    https://forum.unity.com/forums/visual-scripting.537/

    But because your post was an inappropriate followup to a 2-year-old thread, there's no easy way for a moderator to just MOVE it to where it should be.

    You should consider re-posing your question in the visual scripting tutorial.

    And please ... DON'T followup on someone else's thread. It's not like they charge you for making a new thread.

    Make a new post, and make it in Visual Scripting for visual scripting problems.

    How to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220
     
Thread Status:
Not open for further replies.