The goal is to have the player enter a triggerbox, then be allowed to press the “E” key to destroy an object and spawn a new one.
This is the code I have for when the player collides with the triggerbox
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwitchCubeScipt : MonoBehaviour
{
public GameObject goodcube;
public GameObject badcube;
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.name == "Cylinder")
{
Debug.Log("collided");
if (Input.GetKeyDown(KeyCode.E))
{
Debug.Log("key pressed");
Destroy(badcube);
Instantiate(goodcube, transform.position, Quaternion.identity);
}
}
}
}
This code is a component on the triggerbox
Nothing shows up in the console so the, collison with the trigger isn’t working.
Here is the scene. “GoodCube” is a prefab the cylinder is the player and the black cube is “BadCube”
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwitchCubeScript : MonoBehaviour
{
public GameObject goodcube;
public GameObject badcube;
void OnTriggerStay(Collider other)
{
if (Input.GetKey(KeyCode.E) && badcube)
{
Debug.Log("key pressed");
Destroy(badcube);
Instantiate(goodcube, transform.position, Quaternion.identity);
}
}
}