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 /
avatar image
0
Question by Confused · Aug 05, 2011 at 06:29 PM · booleanreference

Can't reference boolean in another script

I am trying to reference a boolean in another script attached to a different game object. But I keep on getting the error "It is not possible to invoke an expression of type 'boolean'".

The referencing script:

 function Update () { 
      var otherScript: OtherScript = GetComponent(OtherScript); 
      otherScript.DoSomething() = true; 
 }

The referenced script:

 private var DoSomething : boolean;
 
 function Update(){
     
     if(DoSomething) {
     transform.Rotate(0,0,60    * Time.deltaTime);    
     }    
 }

This a simplified script I put together, the script I'm really trying to figure out is a lot more complicated but the basic issue remains.. I've been trying to tackle this ridiculously simple problem for a week now, I'm still very new to programming..

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

3 Replies

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

Answer by Eric5h5 · Aug 05, 2011 at 06:40 PM

You can't assign a value to a function. But DoSomething is a variable, not a function, so just refer to it like other variables.

Comment
Add comment · Show 1 · 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 Confused · Aug 05, 2011 at 10:21 PM 0
Share

I see, but how would I reference a variable?

avatar image
3

Answer by Meltdown · Aug 05, 2011 at 06:50 PM

DOSomething is a variable, not a function.

So you would reference it as

 otherScript.DoSomething = true; 

You may also want to not mark the variable as private.

So change the variable declaration to :

 var DoSomething : boolean;
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
avatar image
1

Answer by SisterKy · Aug 05, 2011 at 06:52 PM

As Eric says. This is how it has to look:

The referencing script:

function Start () { var otherScript: OtherScript = gameObject.Find("TheOtherObject").GetComponent(OtherScript); //you tried to GetComponent without telling where to look, so 'otherScript' was 'null' } // Note: Find() and GetComponent() are expensive. // And the path to OtherScript won't change during the scene. // So we don't need to find it anew every frame. // So we do the referencing in Start() instead of Update(),

function Update () {
otherScript.doSomething = true; // unity didn't realize that 'otherScript' being 'null' would be a problem yet, // because first it had to worry about the (). // It tried to find a function called 'doSomething()' in the referenced Script, // instead of a variable 'doSomething' }

The referenced script:

var doSomething : boolean; // the lower-case isn't mandatory to work but it's good habit

function Update(){

if(doSomething) { transform.Rotate(0,0,60 * Time.deltaTime);
}
}

Here, have a look at this: http://unityscript.com/
bare with the 'for super-dummies'-approach. There's some very valuable information there =)

Edit: Meltdown was faster...
Ooops... missed the 'private' Meltdown has pointed out. now fixed.

Greetz, Ky.

Comment
Add comment · Show 7 · 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 Meltdown · Aug 05, 2011 at 06:57 PM 0
Share

@Sister$$anonymous$$y oh master scripter and tag editor :) won't compile in your script lol :):)))

avatar image Meltdown · Aug 05, 2011 at 06:58 PM 0
Share

Let me repeat that... you have html tags in your script lol

avatar image Confused · Aug 05, 2011 at 08:41 PM 0
Share

Thanks for all the advice! :) it's still broken though. I tried it out and now I get the error " :NullReferenceException: Object reference not set to an instance of an object ".
I tried to reference the object with a tag using:

var taggedObject = GameObject.FindGameObjectsWithTag ("object");

function Update () { var otherScript: OtherScript = GetComponent(OtherScript); taggedObject.otherScript.doSomething = true; // the () here were the problem }

but the same error persists. I also get :ArgumentException: FindGameObjectsWithTag can only be called from the main thread.: now.

avatar image SisterKy · Aug 05, 2011 at 08:46 PM 1
Share

(do you see my post formatted properly?)

no, no, that won't work... but now that you mention it, I should have spotted this one. The problem is

 var otherScript: OtherScript = GetComponent(OtherScript);

Unity doesn't know where to look for that script, so it assigns 'otherScript' 'null'. Then in the next line you try to access otherScript but it can't do that so that line gives you the nullreferenceexception. try:

 var otherScript: OtherScript = gameObject.Find("NameOfTheObject").GetComponent(OtherScript); 

(drat! for some reason I don't get e-mail-notifications for this thread =/ )

Greetz, $$anonymous$$y.

avatar image Eric5h5 · Aug 05, 2011 at 09:02 PM 0
Share

@Confused: you can't run code outside functions, you can only declare variables. "Doing stuff" like FindGameObjectsWithTag can only be done in functions.

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

6 People are following this question.

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

Related Questions

GetComponent error: Object Reference not set to an instance of an object. 1 Answer

Boolean while looking at a game object? 1 Answer

Can I get a reference (not a copy) to a string from a script? 2 Answers

Setting up reference to an inactive GameObject through script 1 Answer

Player Status Boo with Kimmons Dialogue Generator 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