hi all, (beginner level)
I’m trying to spawn a character with button press e and despawn it with the same key by checking if it exists in scene or not. If it does not exist in scene and e is pressed, it should spawn the characer, if it does exist in scene and e is pressed it should despawn the character.
I’m having trouble with the check if in scene part. Spawning works if I remove the game object find part, but it does not spawn at all with the check in place in the if statement. (the object is not present in the scene/hierarchy by default, so that should not be the problem).
How do I go about fixing this? Thks!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CharController001 : MonoBehaviour
{
public GameObject PlayerChar001;
void Update()
{
if (Input.GetKeyDown(KeyCode.E) && GameObject.Find("PlayerChar001") != null)
{
Instantiate(PlayerChar001, GameObject.Find("UtilitySpawnChar001").transform.position, Quaternion.identity);
}
}
}