- Home /
game object set active on trigger enter?
I'm trying to make a jetpack pick up object, so that when you run into the jetpack that is laying on the ground, it destroys from the ground, and then i want to set my jetpack gameobject to active, that i have on my player, which ive set to false as default so its not showing. Ive got the code attached to the jetpack object, but its not working, just giving me a null reference exception, am i missing getcomponent somewhere? please help, thx :)
Heres my code as following:
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
GameObject go = GameObject.FindGameObjectWithTag ("JetPack");
go.SetActive (true);
Destroy (gameObject);
}
}
Wow, weird i didnt think of that! that worked perfectly, thanks alot man :) except i changed the code abit and did GameObject.FindGameObjectWithTag ("JetPack").renderer.enabled = false;
in the awake and then GameObject.FindGameObjectWithTag("JetPack").renderer.enabled = true;
in the ontrigger method :P thanks again!
Answer by Hexer · Jul 28, 2014 at 12:33 AM
I don't think it is possible to activate an object when it is inactive (sound stupid but I tried it out) the gameobject has to be checked to let the script work.
Instead you could just de-activate the render of the jetpack on the player and when the player collide with the jetpack on the ground the render is set to true.
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player")
{
GameObject.Find("JetPack").GetComponent(Renderer).enabled=true;
//activate the JetpackScript
gameObject.GetComponent<TheJetpackScript>().enabled = true;
// Destroys the jetpack that is on the ground
Destroy (gameObjectWithTag("JetpackOnTheGround"));
}
}
Wow, weird i didnt think of that! that worked perfectly, thanks alot man :) except i changed the code abit and did GameObject.FindGameObjectWithTag ("JetPack").renderer.enabled = false; in the awake and then GameObject.FindGameObjectWithTag("JetPack").renderer.enabled = true; in the ontrigger method :P thanks again! (just repasted the comment) i will set this as answer, thanks :)
Answer by RoyalDio2 · Apr 04, 2016 at 09:07 AM
using UnityEngine; using System.Collections;
public class PlayerController : MonoBehaviour {
public GameObject cubes;// Object to active
public float sec = 14f;
void OnTriggerEnter(Collider other)
{
if (other.tag == "JetPack") { //tag name Object to Pick Up
cubes.SetActive (true);
StartCoroutine (LateCall());
Debug.Log ("JetPack Ready");
}
}
//after same sec Object to false
IEnumerator LateCall()
{
yield return new WaitForSeconds (sec);
cubes.SetActive (false);
Debug.Log ("JetPack Diactive!");
}
}
Your answer
Follow this Question
Related Questions
Problems with save 1 Answer
ReActivate object with null movement ?? 2 Answers
Need workaround for this line of code 1 Answer
GameObjects that I deactivate are reactivated immediately 0 Answers