WARNING: You are probably about to witness a extreme amount of stupidity because I am trying to make my first game ever. So I am attempting at trying to make a puzzle game and what I am working on right now is a switch that will deactivate the door when on and reactivate the door when off. Here is my code for my switch:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwitchController : MonoBehaviour
{
public bool isOn = false;
public GameObject ClosedDoor;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
private void OnTriggerStay2D(Collider2D other)
{
if (other.tag == "Player" && Input.GetKeyDown(KeyCode.Space) && isOn == false)
{
isOn = true;
}
else if (other.tag == "Player" && Input.GetKeyDown(KeyCode.Space) && isOn == true)
{
isOn = false;
ClosedDoor.SetActive(true);
}
}
}
And here is my code for the door:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ClosedDoorController : MonoBehaviour
{
public GameObject Player;
public SwitchController Switch;
// Start is called before the first frame update
void Start()
{
GameObject g = GameObject.FindGameObjectWithTag("Switch");
Switch = g.GetComponent<SwitchController>();
Collider2D z = Player.GetComponent<Collider2D>();
}
// Update is called once per frame
void Update()
{
if(Switch.isOn == true)
{
gameObject.SetActive(false);
}
}
}
My code won’t work, and I don’t know why… please help!