gameObject.SetActive not working?

Here’s my code. Clicking does nothing. Any ideas?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Flash : MonoBehaviour {

    // Use this for initialization
    void Start () {
       
        gameObject.SetActive(false);
    }
   
    // Update is called once per frame
    void Update () {
       
        if(Input.GetButtonDown("Fire1")){
            gameObject.SetActive(true);
        }

        if(Input.GetButtonUp("Fire1")){
            gameObject.SetActive(false);
        }
    }
}

Once it’s inactive, it stops running the code to turn itself back on.

6 Likes

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().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.

3 Likes

I had this problem for so long and somehow changing the object name fixed it.

2 Likes

Had the exact same problem, and this fixed it as well.

2 Likes

same with me

How did you do that tho?

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://discussions.unity.com/t/481379

2 Likes

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).

5 Likes

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.

3 Likes

@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.

3 Likes

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.

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.

1 Like

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’.

7789743--983595--2.png

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