- Home /
How To Keep GameObjects From Enabling Again?
Sorry for the second question of today, please excuse me. As you read in the title, how can I keep GameObjects From Enabling Again after they have already been destroyed? In my game, I fade into the scene but with the same scene, not from a different scene. It plays a little animation and then fades. Before it fades the objects get destroyed. What I want to do, is after it get's destroyed, to never spawn it again unless the Player restarts the game or something, because if I clicked a button and went to a different scene and then clicked back, it would play the little animation and have to destroy the objects again. So how can I keep certain gameobjects from reactivating and from coming back after being destroyed or disabled?
By the way, I use GameObject.active to get rid of the gameobjects I want in this scene.
if you keep loading in to the same scene it will keep giving you the same objects in that scene. if you need the objects only once and have some sort of game manager, instantiate them, destroy them, never instantiate again until the next playthrough
That or have 2 scenes that are the same except one has the objects already deleted. Just save your current scene as "$$anonymous$$ySceneB" or something and remove the things you dont want. load into that scene
I don't have some sort of game manager. All I have is this script:
#pragma warning disable 0618
using UnityEngine;
using System.Collections;
public class ActivateScript : $$anonymous$$onoBehaviour {
public GameObject BScreen;
public GameObject ScreenFader;
public GameObject Light;
public $$anonymous$$ain$$anonymous$$enu HOLDER;
public GameObject Name;
void Start()
{
HOLDER = GetComponent<$$anonymous$$ain$$anonymous$$enu> ();
StartCoroutine (ScriptEnable ());
BScreen.active = true;
ScreenFader.active = false;
Light.active = true;
StartCoroutine (ScreenDisable ());
StartCoroutine (NameEnable ());
}
IEnumerator ScreenDisable()
{
yield return new WaitForSeconds (7.0f);
BScreen.active = false;
Light.active = false;
ScreenFader.active = true;
}
IEnumerator NameEnable()
{
yield return new WaitForSeconds (4.9f);
Name.active = true;
}
IEnumerator ScriptEnable()
{
yield return new WaitForSeconds (14.0f);
HOLDER.enabled = true;
}
}
Isn't there some code to "never instantiate again until the next playthrough?"
the easiest solution imo is to just save your current scene as another name, delete the objects you only want once in your first scene, and load into the second. they are essentially the same scene just without those objects
by delete i mean manually remove them from the second scene
I just read your edit, not a bad idea. I'll see if that works. Thanks.
I just tried it, thank you it appears to work very well. You should post it as an answer so that I can accept it. Thanks again.
Answer by Dogg · Sep 14, 2014 at 11:17 PM
The answer is making a copy of the scene I want, and getting rid of the game objects that I don't need. Then you load the scene when you need it. This answer was from b1gry4n, so thank him/her.
Answer by rafaelbarretobra · Jan 17, 2017 at 08:15 AM
I have a better solution. When the player gets an item in my game, I use it's position Vector3 as a unique float for that item, for example: the player gets an iron sword (after that, I want that sword gone for good).
So, in the Iron Sword script, I do:
bool isDestroyed = false;
float uniqueID = this.transform.position.x+this.transform.position.y+this.transform.position.z;
void Start(){
if (PlayerPrefs.GetFloat(uniqueID) == 1) Destroy(this);
}
void GetSword(){ PlayerPrefs.SetFloat(uniqueID, 1); }
I know it's not so elegant, but I'm not an elegant programmer :D
Contact me if you need more help.
MP Game Studios mundiproject@hotmail.com