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?