scripting SetActive(true_false)?
Hey guys i have a problem with getting a deactivated gameobject to be reactivated
When i enter my atv everything works fine accept for the player it is still in the scene but turned off. my exit script can`t find it when i press F so how can i fix this?
using UnityEngine;
using System.Collections;
public class Exit_ATV : MonoBehaviour {
public GameObject Player;
public GameObject ATVplayer;
public GameObject ATV;
public GameObject Trigger;
public GameObject TriggerATV;
// Use this for initialization
void Start () {
Player = GameObject.FindWithTag("Player");
Player = GameObject.Find("Player");
ATVplayer = GameObject.FindWithTag("ATVplayer");
ATV = GameObject.FindWithTag("ATV");
Trigger = GameObject.FindWithTag("Trigger");
TriggerATV = GameObject.FindWithTag("TriggerATV");
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.F))
{
Player = GameObject.Find("Player");// Here is the problem! Player cant be found becouse it is set to SetActive(false) by my enter atv script
Player.SetActive(true);
ATVplayer.SetActive(false);
ATV.SetActive(true);
Trigger.SetActive(true);
TriggerATV.SetActive(true);
}
}
}
1) take reference of Enter_ATV script in this and access player object of Enter_ATV which you found.
2) take reference of Exit_ATV in Enter_ATV and assign player reference of Exit_ATV when you find it in Enter_ATV.
3) keep an empty parent of player find that parent and use transform.getChild to get disabled child object.
Hi, use "Input.Get$$anonymous$$eyDown" ins$$anonymous$$d of "Input.Get$$anonymous$$ey"
Answer by saschandroid · Aug 24, 2016 at 11:52 AM
There are multiple solutions to this ... here are 2:
Drag the GameObjects to the slots of the script (they are all public) and don't use GameObject.Find() in Start(). or
Leave all the GameObjects active in the Inspector, use GameObject.Find() in Start() and deactivate the unwanted directly after finding them (also in Start() )