Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Ishkur · May 29, 2013 at 05:56 PM · c#getcomponentvariables

Syntax troubles (GetComponent and changing variables)

Hello. It seems a lot of other people have trouble accessing and changing variables in other scripts/components however I've managed to make myself a problem that the other answers I've read weren't about to address.

I have a bool I want to change that lies on a different script. Both the script making the change and the script containing the changing variable lie on the same gameObject.

Can someone point out what I'm doing wrong? I'm working in C#

 var go = this.gameObject;
 go.GetComponent("PlayerStateSync").moving = false;

That tells me UnityEngine.Component doesn't contain a definition for the "moving" bool I'm trying to reference.

 var go = this.gameObject;
 var com = go.GetComponent("PlayerStateSync");
 go.com.working = false:

That tells me UnityEngine.GameObject can't find a definition for the bool.

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
1
Best Answer

Answer by hiddenspring81 · May 29, 2013 at 06:04 PM

Try this instead

 var go = this.gameObject;
 var com = go.GetComponent<PlayerStateSync>();
 com.working = false;

Or if you want to be really fancy

 GetComponent<PlayerStateSync>().working = false;

This assumes that PlayerStateSync is a subclass of MonoBehaviour.

Comment
Add comment · Show 4 · 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 Ishkur · May 29, 2013 at 06:11 PM 0
Share

What? Really?

Why does that work? It looks almost exactly the same as my attempt!

avatar image hiddenspring81 · May 29, 2013 at 06:30 PM 1
Share

There are three different version of GetComponent(). The version that you've used GetComponent("PlayerStateSync") will return an object of type Component, which you'd then have to cast into an instance of PlayerStateSync,

 var com = GetComponent("PlayerStateSync") as PlayerStateSync;

If you want to avoid having to perform the casting operation, then you can use the generic version of GetComponent()

 var com = go.GetComponent<PlayerStateSync>();

These two version are identical, it's just that the second option is a bit cleaner and more readable.

avatar image Ishkur · May 29, 2013 at 06:34 PM 0
Share

Ahhhh ok. Yes I see it in the documentation now that you've explained it.

function GetComponent. () : T function GetComponent (type : String) : Component

It's not apparent to me what those mean so I've just been glossing over it <- noob.

So now I can better read an API reference, thanks!

avatar image hiddenspring81 · May 29, 2013 at 06:36 PM 0
Share

No problem. Happy coding!

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

14 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

Related Questions

Using a variable value with GetComponent 1 Answer

fix script to get C# var from another object instead of object this script is attached to 2 Answers

C# Access to a variable by another script attached in a gameObject in the scene. 2 Answers

Calling variable accross scripts in C# problem (Closed) 1 Answer

Getting a Variable on Script B to equal a variable from script A 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