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.