How to turn off/on a collider using a bool variable from another script
Hi, I'm new to C# and am struggling to get my head around some of the syntax.
What I want to do is turn off a capsule collider which is on my ground object, but only if the player is in a certain 'form'
I have created 2 Scripts.
One is for my Player. Within this player script there is a public bool called 'IsBlue'. This is toggled on/off with the return key.
I then have another script attached to a ground platform. This platform has 2 colliders. one for the actual surface of the platform which acts as the actual ground. Another which is a capsule collider which surrounds the platform. This Capsule collider has a material with Bounce properties attached to it. The Capsule collider is the one I want to switch off if the 'IsBlue' bool from my player script is true.
I have attached the relevant code from each script below.
My Players Script (The game object it's assigned to is called PlayerObj. The scripts name is Player):
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
//Bool to identify if blue
public bool IsBlue = false;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
//button press to toggle blue on/off
if (Input.GetKeyDown(KeyCode.Return))
{
IsBlue = !IsBlue;
}
}
}
My Platform script(Attached to the game object Redplatform);
using UnityEngine;
using System.Collections;
public class RedMagnet : MonoBehaviour
{
private CapsuleCollider capsuleCollider;
private Player player;
void Sart()
{
capsuleCollider = GetComponent<CapsuleCollider>();
player = GameObject.FindGameObjectWithTag("PlayerObj").GetComponent<Player>();
}
void Update()
{
// check if the player is blue and do something
if (player.IsBlue)
{
capsuleCollider.enabled = false;
}
else
{
capsuleCollider.enabled = true;
}
}
}
The scripts have been attached to their relevant game objects and the game runs. However the capsule collider is not witching on or off? I made the 'IsBlue' bool public so I can see it toggling on/off. that is working. But the capsule collider is just staying on. The only error i'm getting is
'NullReferenceException: Object reference not set to an instance of an object RedMagnet.Update () (at Assets/Code/Scripts/RedMagnet.cs:18)'
RedMagnet.cs is the name of the script for the platform.
Any ideas?
Why not just make that capsuleCollider variable public and drag on the object with the Capsule Collider you want to affect onto the slot in the inspector. It'll only accept an object that actually has a Capsule Collider.
Hi there, Thanks for the response, but that didn't seem to work:(
When toggling the 'IsBlue' Bool on / off the Capsule collider still just stays active.
Same error: NullReferenceException: Object reference not set to an instance of an object
Ah $$anonymous$$y bad, made the Player variable also public and manually assigned the Player object. This got it working :)
Is there anything in that code no longer needed, just so I can tidy it up?
Thanks for pointing in the right direction :)
I'd need to see your updated script but honestly ... your first few Unity games will never be games you can put out commercially. Or, more to the point, never should put out (because it might give your company name a bad vibe).
Basically don't worry too much about redundant code until you can confidently ignore/better most of the Answers on Unity Answers.
You will get there, I'm not there yet BTW, but I can see the place where I will create a commercial game getting closer and closer.
Good luck mate :D
Your answer
Follow this Question
Related Questions
Problems with debug and if-statement 1 Answer
In void bool dont changing 1 Answer
If statement not working 1 Answer
Why is my bool being set to true when program is run? 1 Answer
Check a bool from a prefab? 0 Answers