How to turn turn off/on a collider with a boolean from another script
Hi, I'm rather new to unity and C#.
I am trying to set up something that will allow me to toggle a material assigned to a box collider on/ off.
Basically I have a boolean variable assigned to my Player called IsBlue. I then have a box collider assigned to my Ground object. Within this box collider I have assigned a material which will make my player bounce when in contact with it.
What I want to do is switch these bouncing physics on/off depending on if my IsBlue variable is set to true or not.
For example If IsBlue is set to false then the box collider should be enabled. If IsBlue = True then the box collider should be disbaled.
I'm pretty sure this is a simple task however i'm new to C# and can't make sense of anything I've found online. There seems to be examples but no explanation of how to use the example.
I understand the concept of coding but am unfamilliar with the syntax. I essentially need to know: How to reference the IsBlue Bool from my other Player.cs Script and how to then create an if Statment to get the results above.
Could anyone help?
Thanks.
Answer by Vencarii · Jun 10, 2016 at 12:52 PM
Not tested, but something like this should do it. (I would change the variable name of isBlue to blue)
Ground Class:
private BoxCollider boxCollider;
private Player player;
void Sart ()
{
boxCollider = GetComponent<BoxCollider> ();
player = GameObject.FindGameObjectWithTag ("Player").GetComponent<Player> ();
}
void Update ()
{
// check if the player is blue and do something
if (player.isBlue ()) {
boxCollider.enabled = false;
}
else {
boxCollider.enabled = true;
}
}
Player Class:
private bool blue;
void Start ()
{
// whatever the initial blue value should be
blue = true;
}
public bool isBlue ()
{
return blue;
}
Ah that's great! This makes sense however I get the error
The member player.IsBlue cannot be used as a method or delegate. I'm probably just being dumb but any ideas?
I've supplied all the relevant code related to my scripts:
Gmae Object= PlayerObj Script=Player
using UnityEngine;
using System.Collections;
public class Player : $$anonymous$$onoBehaviour
{
public bool IsBlue = false;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Return))
{
IsBlue = !IsBlue;
}
}
}
Ground Script
public class Red$$anonymous$$agnet : $$anonymous$$onoBehaviour {
private BoxCollider boxCollider;
private Player player;
void Sart()
{
boxCollider = GetComponent<BoxCollider>();
player = GameObject.FindGameObjectWithTag("PlayerObj").GetComponent<Player>();
}
void Update()
{
// check if the player is blue and do something
if (player.IsBlue())
{
boxCollider.enabled = false;
}
else
{
boxCollider.enabled = true;
}
}
}
You are mixing the variable isBlue with the method isBlue(). You don't have an isBlue() method in your player class. It should work if you add one like in my example. (and return isBlue; ins$$anonymous$$d of return blue; because you didn't rename the variable)
I'm Really sorry but i'm not following you :( I have little experience with C# and it's syntax.
Within my Player class i have created an IsBlue Variable as you've seen and then created and If statement to make the return key toggle it on/off
When it's off I want that to turn on the collider.
I don't understand what this is actually doing?:
private bool blue;
blue = true;
}
public bool isBlue ()
{
return blue;
}
I do understand:
void Update()
{
// check if the player is blue and do something
if (player.IsBlue())
{
boxCollider.enabled = false;
}
else
{
boxCollider.enabled = true;
}
}
but i'm clueless as to why it's not working. I don't really understand what you are saying I've done wrong.
You'll have to forgive me for being naive on this one :(
Add this code to your player class:
public bool isBlue ()
{
return isBlue;
}
And then you should read something about classes and variables to learn the differences. ;)
I appreciate the help but this isn't working for me. I've added this in as suggested and added the other code and updated the player objects etc but it will not run.
If I get it working I'll let you know
I apologies for being a pain in the backside. I really can't seem to get this to work.
As I say I understand the concept of coding but not the syntax. Would you be able to supply another example but with an explanation.
I've tried implementing what you've suggested in multiple different way but I', getting different errors every time. I'm really stumped and don't know what to do.
When you say Player class you do mean the script used for my player right? Also I'm lost as to what is what, We have mentioned Blue, isBlue and IsBlue. Just for the record the working Bool I have created in my player script is IsBlue.
All I want to do is target the IsBlue bool from my player script and if it is false, set boxCollider.enabled = false;
I don't understand what the isBlue is being used for?
$$anonymous$$aybe im being stupid or getting confused but i don't get why we're creating another bool in the player script? If you could supply another example and explain that'd be great.
No worries if not, i've already took enough of your time and I appreciate your first response even if I didn't understand :S
Yeah, I guess your called your script Player.cs and added it to your player GameObject, so that is your player class.
Ok the most simple way to it is this:
Player class:
public bool isBlue;
Ground class:
private BoxCollider boxCollider;
private Player player;
void Sart ()
{
boxCollider = GetComponent<BoxCollider> ();
player = GameObject.FindGameObjectWithTag ("Player").GetComponent<Player> ();
}
void Update ()
{
// check if the player is blue and do something
if (player.isBlue) {
boxCollider.enabled = false;
}
else {
boxCollider.enabled = true;
}
}
I'm not sure what the best practice is in C#, but normally you should set your blue variable to private and use the public IsBlue() method to access the blue variable from other objects.