- Home /
Code not working right for puzzle game
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!
Answer by dragon_script · Apr 22, 2020 at 10:02 PM
First, your code is so good to be your first game. I'll check your code to see any mistakes.
I have been practicing c# code for a while before I actually started to make a game..... I have spent at least 3 hours on the forums making this code even with my years of program$$anonymous$$g experience..... This isn't my first program but it is my first game....
Answer by yummy81 · Apr 23, 2020 at 11:28 AM
Create 3 gameobjects. One for Player, one for Door and one for Switch. Create 2 scripts: SwitchController, and Player. Attach components to each gameobject as follows:
-
1. Player
Rigidbody2D
CircleCollider2D (or any other kind of 2D collider)
Player (Script)
-
2. Switch
BoxCollider2D (or any other kind of 2D collider)
SwitchController (Script)
Now, select the Switch gameobject. Look at the collider2D and check "Is Trigger". Then, look at Switch Controller (Script) and drag and drop Door gameobject into Door slot.
Next, select the Player gameobject. Look at the Rigidbody2D, find "Sleeping Mode", and choose "Never Sleep". This is very important, because otherwise OnTriggerStay2D will not work! Next, find Body Type and choose "Kinematic". Now, look at the Player (Script) and drag and drop Switch gameobject into Switch Controller slot. Finally, assign tag "Player" in the inspector.
Ok. That's it. Scripts below. Enjoy!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public SwitchController switchController;
public float speed = 8f;
void Update(){
Vector3 pos = new Vector3(
Input.GetAxisRaw("Horizontal"),
Input.GetAxisRaw("Vertical"),
0f
);
pos = pos.normalized * speed * Time.deltaTime;
transform.position += pos;
if (Input.GetKeyDown(KeyCode.Space)){
switchController.spacePressed = true;
}
}
}
and the second script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SwitchController : MonoBehaviour
{
public bool spacePressed;
public bool isOn;
public GameObject door;
void OnTriggerStay2D(Collider2D other){
if (other.CompareTag("Player") &&
spacePressed){
spacePressed = false;
isOn ^= true;
door.SetActive(!isOn);
}
}
}
Your answer
Follow this Question
Related Questions
ENEMY MOVEMENT SCRIPT 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
OnCollisionEnter Logic Error 1 Answer