Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Pierce95 · Jun 10, 2016 at 12:10 PM · colliderbooleanif-statements

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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;
 }
Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Pierce95 · Jun 10, 2016 at 01:41 PM 0
Share

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;
     }
 }

}

avatar image Vencarii · Jun 10, 2016 at 01:59 PM 0
Share

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)

avatar image Pierce95 Vencarii · Jun 10, 2016 at 02:21 PM 0
Share

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 :(

avatar image Vencarii · Jun 10, 2016 at 02:30 PM 0
Share

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. ;)

avatar image Pierce95 Vencarii · Jun 10, 2016 at 02:41 PM 0
Share

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

avatar image Pierce95 Vencarii · Jun 10, 2016 at 06:23 PM 0
Share

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

avatar image Vencarii Pierce95 · Jun 10, 2016 at 08:03 PM 0
Share

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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

If script not working as expected - camera switch 1 Answer

How to turn off/on a collider using a bool variable from another script 1 Answer

AudioSource in Update 1 Answer

bool not properly working and time counter only adding once 1 Answer

Only the very first if statement actually works 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges