• 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 /
avatar image
0
Question by SHG · Mar 15, 2014 at 01:04 AM · booleanconverttogglecannot

Cannot convert 'boolean' to 'somevariable'.

So I've got a problem I'm getting this compiler error in unity saying "Cannot convert 'boolean' to 'Player_Controller'." and I don't know why? I'm trying to create a toggle scripts script that accesses two java scripts in the same object that this script is attached to. One is called "Free_Look" and the other is called "Player_Controller", both are java scripts so I don't have to worry about calling a C# script. Script below. Sorry if the script sucks I'm not a good programmer just trying to learn problem by problem.

 var toggle : boolean = false;
 var script1 : Free_Look;
 var script2 : Player_Controller;
 
 function Start() {
      script2 = GetComponent(Player_Controller).enabled = true;
      rigidbody.useGravity = true;
      script1 = GetComponent(Free_Look).enabled = false;
      Time.timeScale = 1;
 }
 
 function Update() {
      if (Input.GetKeyDown("f")) {
           toggle = !toggle;
 
      if (toggle) {
           print("Toggled ON");
           script1 = GetComponent(Free_Look).enabled = true;
           script2 = GetComponent(Player_Controller).enabled = false;
           rigidbody.useGravity = false;
           Time.timeScale = 0.5;
      }
 
 
      else {
           print("Toggled OFF");
           script2 = GetComponent(Player_Controller).enabled = true;
           rigidbody.useGravity = true;
           Script1 = GetComponent(Free_Look).enabled = false;
           Time.timeScale = 1;
 
      }
 } 
 }
Comment
Add comment · Show 1
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 Benproductions1 · Mar 15, 2014 at 01:18 AM 0
Share

Please indent your code properly, it's really difficult to read and will become impossible once code complexity increases.

2 Replies

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

Answer by Benproductions1 · Mar 15, 2014 at 01:24 AM

Since you didn't post what line the error is on, I'll assume it happens on a line like line 6:

 script2 = GetComponent(Player_Controller).enabled = true;

In this line, you are assigning to both script2 and GetComponent(Player_Controller).enabled a value of true, which of type boolean.

However, if we expand the types of each element, we can see a problem:

 Player_Controller = Component->boolean = boolean;

As you can see there is a problem with this: You are assigning a boolean type to a Player_Controller type variable. What you probably expect it to do is assign GetComponent to script2 and then enable it. Simply describing it is already a two step process and the same goes for the code. You cannot do this in one line.

Comment
Add comment · Show 12 · 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 SHG · Mar 15, 2014 at 01:37 AM 0
Share

alt text

screenshot.png (61.8 kB)
avatar image SHG · Mar 15, 2014 at 01:43 AM 0
Share

Thanks I understand why I got the error now, although how would one go about doing what I'am trying to do in multiple lines?

avatar image infinitypbr · Mar 15, 2014 at 01:45 AM 0
Share
 script2 = GetComponent(Player_Controller);
 script2.enabled = true;
avatar image SHG · Mar 15, 2014 at 01:50 AM 0
Share

Thank you so much! You've really helped me out a lot!

avatar image Benproductions1 · Mar 15, 2014 at 01:51 AM 0
Share

On the first line you get the component and assign it to a variable, while on the second line you disable or enable that component. ie:

 comp = GetComponent(Type);
 comp.enabled = boolean;

In you code you should only ever use GetComponent in either Awake or Start, so you don't have to call it again during update (it's quite a slow function). Then you can simply enable or disable it during Update without using GetComponent.

Show more comments
avatar image
0

Answer by infinitypbr · Mar 15, 2014 at 01:23 AM

Yes, please indent the code -- that would be helpful.

But if I'm not mistaken, the lines like this are incorrect:

 script2 = GetComponent(Player_Controller).enabled = true;

If script2 is a variable holding the Player_Controller script, you should just be able to say script2.enabled = true; once the variable is set. So make two lines -- first set the variable, then enable the script.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Two toggles, only one can be on, both can be off 3 Answers

Toggle/flicker bool script - Please help 1 Answer

Converting Variables 3 Answers

mecanim boolean not working 1 Answer

C# Cannot implicitly convert type UnityEngine.UI.Selectable to UnityEngine.UI.Selectable 2 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