- Home /
Reactivate GameObjects that are disabled not working??
Hey guys im trying to exit the turrent but my script can not find or activate my player and my off turrent
how can i fix this ??
using UnityEngine;
using System.Collections;
public class ExitTurrent : MonoBehaviour
{
public GameObject Turrent;// the player turrent
public GameObject Player; // the player
public GameObject TurrentOff;// the scene turrent
public bool inTurrent = true;
EnterTurrent turrentscript;
GameObject guiObj;
AudioSource source;
public AudioClip audio;
// Use this for initialization
void InTurrent()
{
inTurrent = true;
Player = GameObject.Find("_SpartanPlayer");// Find Player
Player.SetActive(false);
Turrent.SetActive(true);
TurrentOff.SetActive(false);
}
void Update ()
{
if (Input.GetKeyDown(KeyCode.E))
{
Player = GameObject.Find("_SpartanPlayer");// Find Player
TurrentOff = GameObject.Find("UNSC Warthog MK1");
SendMessage("GetOut");
Debug.Log("Getout");
}
}
// Update is called once per frame
void GetOut()
{
{
Player = GameObject.Find("_SpartanPlayer");// Find Player
Player = GameObject.FindGameObjectWithTag("Player");// Find Player
TurrentOff = GameObject.Find("UNSC Warthog MK1");// Find Player
Player.SetActive(true);
BroadcastMessage("Playertrue");
Player.transform.parent = null;
TurrentOff.transform.parent = null;
Turrent.SetActive(false);
TurrentOff.SetActive(true);
turrentscript.enabled = false;
inTurrent = false;
}
}
}
Answer by Cherno · Nov 07, 2016 at 05:34 PM
GameObject.Find won't find disabled GameObjects. That means that if you don't have a reference to them, they are gone forever. What you can do is either keep the reference in a variable, make the disabled GameObject a child of another GameObject so you can find the parent and find the child´from that, or not disable the whole GameObject, but instead only those components neccessary, such as Renderers, Colliders, and scripts.
Answer by Cynikal · Nov 07, 2016 at 05:58 PM
Your code is already there. You're already setting the "player" variable.
Remove the "Player = GameObject.Find("_SpartanPlayer");// Find Player" from your Update & GetOut. You're already setting it in "GetIn". Since it's already set, then when you try to find a disabled game object, you're setting it back to null.
Your answer
