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);
}
}
}
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.
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).
@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.
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.
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’.
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: