- Home /
GameObject Wont' Set Active,Set active doesn't work trying to do something simple.
I've been working on this for quite a long time. I'm familiar with unity but working on my C# skills. These seem like they should be simple actions to do, but after days and nights looking at this script, it won't work.
All I'm trying to do is display a scene with an active object, then show it for 40 seconds, hide it, and show a new object.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadWeatherShelf : MonoBehaviour {
//Todo
//Hide Object 1 after 40 seconds
//Display A new object
// Use this for initialization
void Start () {
Invoke("NextObject", 40);
}
// Update is called once per frame
void Update () {
}
void NextObject ()
{
GameObject.Find("PrimarySceneContent").SetActive(true);
}
Any help is appreciated!
,I spent a lot of time working on this nights and days and hours and still can't get this to work right. It's some dumb mistake I'm sure.
I'm simply trying to deactivate one object (not destroy), after a set duration 40seconds, and then turn on another object that is de-activated. I've tried so many varieties. Nothing works.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoadShelf : MonoBehaviour {
//Todo
//Hide Object 1 after 40 seconds
//Display A new object
// Use this for initialization
void Start () {
Invoke("NextObject", 40);
}
// Update is called once per frame
void Update () {
}
void NextObject ()
{
GameObject.Find("PrimarySceneContent").SetActive(true);
}
Answer by hrishihawk · Mar 01, 2018 at 07:28 AM
The problem is you're trying to find an object which is already inactive in the scene. So you should be getting a NullReferenceException
after 40 seconds. So what you want to do is to have a reference of the object you want to activate in the script already instead of trying to find it using GameOject.Find
. You can modify your script as follows :
public GameObject objectToActivate ;
And in your NextObject method :
void NextObject ()
{
objectToActivate.SetActive(true);
}
Hi @imaginethepoet. As @hrishihawk said, when you deactivate an object it does not appear on the Scene neither on the hierarchy. So you have to preemptively take an instance of it which will still "exists" whatever you do with the object, albeit not destroying it. You could watch the activate/deactivate tuto video from Unity, it is well explained how deactivation actually works.
Your answer
Follow this Question
Related Questions
Find one inactive player (gameobject) 2 Answers
How to access a game object from a different scene? 0 Answers
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
Find an inactive game object 12 Answers
Delay "Destroy(gameObject)" 2 Answers