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 12, 2016 at 10:12 AM · colliderbooleanboolif statementif else

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?

Comment
Add comment · Show 4
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 Mmmpies · Jun 12, 2016 at 12:19 PM 0
Share

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.

avatar image Pierce95 Mmmpies · Jun 12, 2016 at 08:28 PM 0
Share

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

avatar image Pierce95 Mmmpies · Jun 12, 2016 at 08:33 PM 0
Share

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

avatar image Mmmpies Pierce95 · Jun 12, 2016 at 09:05 PM 0
Share

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

1 Reply

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

Answer by Pierce95 · Jun 15, 2016 at 05:07 PM

MMmpies answered this for me :)

Comment
Add comment · 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

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

67 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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