- Home /
How to keep objects disabled after reloading the scene?
I've just started kind of a first-person shooter and got the following problem: I want to add magazines to the scene. When the Player look at them and press "E", the object should disappear. I already got that. The problem now is, if I leave the room and enter another, then re-enter the first one, I want that the already grabbed magazines don't appear anymore.
At the moment the script attached to the magazine-mesh looks like this:
using UnityEngine;
using System.Collections;
public class Ammo_Script : MonoBehaviour {
bool grabbed=false;
void OnStart(){
if(grabbed){
gameObject.SetActive(false);
}
}
public void Grab(){
gameObject.SetActive(false);
grabbed=true;
Debug.Log("grabbed ammo");
}
}
It is accessed by this script attached to the main camera:
[...]
if(Physics.Raycast(gameObject.transform.position,gameObject.transform.forward,out hitinfo,(float)1.5)){
if(hitinfo.transform.name=="Gun_Ammo"){
//Ammo_Script Ammo = (Ammo_Script) hitinfo.transform.gameObject.GetComponent("Ammo_Script");
//Ammo.Grab();
(hitinfo.collider.gameObject.GetComponent("Ammo_Script") as Ammo_Script).Grab();
Debug.Log(hitinfo.transform.gameObject.GetInstanceID().ToString());
Variables.gun_ammo_count++;
}
}
[...]
The Ammo rightly disappears, but if I re-enter the scene, it is shown again. Anyone know how to fix the script? The Ammo-Script exists multiple times.
Answer by Murkas · Jul 25, 2013 at 02:33 PM
Okay, the following script is working for me:
using UnityEngine;
using System.Collections;
public class Ammo_Script : MonoBehaviour {
int own_level;
void Awake() {
DontDestroyOnLoad(this);
Debug.Log("ammo-script: loaded level: "+Application.loadedLevel.ToString ()+" reloaded?: "+Variables.entered_rooms[Application.loadedLevel].ToString());
if(Variables.entered_rooms[Application.loadedLevel]){
Destroy(gameObject);
}else{
own_level=Application.loadedLevel;
}
}
void OnLevelWasLoaded(int level){
if(level==own_level){
gameObject.collider.enabled=true;
gameObject.renderer.enabled=true;
gameObject.rigidbody.freezeRotation=false;
gameObject.rigidbody.useGravity=true;
}else{
gameObject.collider.enabled=false;
gameObject.renderer.enabled=false;
gameObject.rigidbody.freezeRotation=true;
gameObject.rigidbody.useGravity=false;
}
}
public void Grab(){
Destroy(gameObject);
Debug.Log("grabbed ammo");
}
}
Answer by dorpeleg · Jul 25, 2013 at 06:06 AM
The only way you can do that, is if you make some sort of "pickup manager" that will have DontDestroyOnLoad.
The "pickup manager" will remain between scenes and will auto hide all the picked up items.
I feel like an idiot. I've even done this before, just couldn't surface it in my brain lol +1
That sounds good. But I think I don't come around na$$anonymous$$g the objects "ammo_001", "ammo_002", ... ?
can I ask why you are switching scenes anyway?
It's not always needed....
But regardless, how are you placing your ammo boxes? by hand from the editor or by code?
I am switching scenes, cause I don't want one big scene. I want to split it in smaller scenes. I am placing my Ammo Boxes by editor.
You can try placing DontDestroyOnLoad on the ammo script.
This will make the ammo stay between scenes.
You will have all ammo boxes in all scenes, but if you disable them, they should not bother you.