Other
OnCollisionEnter - Enabled C#
I am trying to have something repetitively simple happen, I want the player to be able to walk through a wall with the BoxCollider set inactive, and once they pass though the collider, i want the BoxCollider to be set active and not allow them to go back though it. I have tried a few different combinations, not sure if I should use SetActive or .enabled and I am thinking that OnColissionEnter will do the trick because the wall is thin, but OnCollisionExit may be better.
I just need a little help with this problem as I cannot seem the write the correct C# code that solves my problem.
Here is what I have
using UnityEngine;
using System.Collections;
public class SetWallInactive : MonoBehaviour
{
Private Void OnCollisionEnter(other)
{
If (other.tag = “Player”)
{
this.GetComponent<BoxCollider>().enabled = false;
}
}
}
thank you for any help you can offer.
Answer by Feelnside · Jun 09, 2018 at 05:37 PM
If I understand correctly, it would be better to create a trigger which activates a box collider of the wall. For example: you have a wall with the box collider (inactive, so that a User can go through). Behind the wall you need to add a box collider and set the "IsTrigger" checkbox. Now you need to attach the following script to that trigger.
using UnityEngine;
public class SetWallInactive : MonoBehaviour
{
//this is a BoxCollider of the Wall. Attach them in the inspector
[SerializedField] BoxCollider col_wall;
void Awake()
{
//let's disable the Wall collider at Awake so that a User can go through the wall
col_wall.enabled = false;
}
void OnTriggerEnter(Collider col)
{
if (col.tag == “Player”)
{
//User has touched a Trigger behind the wall. Let's activate the col_wall so that a User is not able to go back
col_wall.enabled = true;
}
}
}
After that assign the Wall collider into the "col_wall" field. I believe it should work in your case.
I will see if that works, it does seem like that would work better. Thank you. I will get back to you shortly if there is an issue.
No problem. Let me know if you have any issues there.
ok....so I have an issue. it is not working. it will not set the box collider active. not sure why. The player character is just the first person player controller that unity gives you. is there something i need to add to that to make this work ?
can you help please. sorry for the late reply.
here is a picture of what i have.
and here is the script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SetWallInactive : $$anonymous$$onoBehaviour
{
[SerializeField] BoxCollider col_wall;
void Awake()
{
col_wall.enabled = false;
}
void OnTriggerEnter(Collider col)
{
if (col.tag == "Player")
{
col_wall.enabled = true;
}
}
}
script is not working, asking a fresh question with more info on the issue.
Follow this Question
Related Questions
C#: How can you make your character jump only when you are touching the top of the object? 1 Answer
Unity2d trying to make an enemy react to an attack with a flash. Why isn't this code working? 1 Answer
My Game Over Canvas is working in one level but not the other! 0 Answers
Unable to reactivate an object that's been deactivated. C# 4 Answers
SetActive delay between code and actual object on the screen 0 Answers