This is my code I try to enable my already disabled gameobject
so it shows on CollisionEnter
using System.Collections.Generic;
using UnityEngine;
public class JumpMessage : MonoBehaviour
{
private GameObject jumptutorialbox;
private GameObject triggerjumptut;
public AudioSource audioPlayer;
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Player"))
{
Debug.Log("Triggered Jump Tutorial");
triggerjumptut = GameObject.Find("TriggerJumpTut");
triggerjumptut.gameObject.SetActive(false);
jumptutorialbox = GameObject.Find("TutorialJUMP");
jumptutorialbox.gameObject.SetActive(true);
audioPlayer.Play();
}
}
}
I want to enable a disabled object on CollisionEnter but I get this error:
NullReferenceException: Object reference not set to an instance of an object
JumpMessage.OnCollisionEnter (UnityEngine.Collision collision) (at Assets/Scripts/JumpMessage.cs:19)
CodePudding user response:
Hey so in unity in order to set an inactive gameobject back to active your gonna have to set it active through another gameobject because if an object in the scene is inactive the code and scripts are never gonna run so create another script and put it on a new gameobject and make a bool or something and that's how you can set it back to active
CodePudding user response:
You can create a new Empty GameObject with the code, with this you can Enable and Disable the other GameObject by code without any problem. I like call this GameObjects with those codes that gestion other GameObject's behavior as Controllers.
CodePudding user response:
Check if something is found when you are using GameObject.Find. Insted of this you can probably use public variable and set reference to object in inspector.