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 Walodcave · Mar 06, 2013 at 12:19 PM · variablevariablesvaluevalues

Changing a public varible from another script

I have a public variable called "moveSpeed" in a script called "CharacterMovement" and I need to edit the values in another script.

I tried:


charControl = GameObject.Find("player").GetComponent(CharacterMovement);

charControl.moveSpeed = 10;


But it gives me and error "Unknown identifier 'CharacterMovement'" and "moveSped is not a member of 'Object'" I am not sure what to do or how to fix it.

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
0

Answer by tiltaghe · Mar 06, 2013 at 01:32 PM

I think you should declare the charControl variable as a CharacterMovement type before assigning it.

 private CharacterMovement charControl; (C#)
 private var charControl : CharacterMovement (js)


Also maybe its ok to access script like that in js, but in C# I think you should write it like

 GetComponent<CharacterMovement>();

Hope it helps :)

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 Walodcave · Mar 06, 2013 at 01:44 PM 0
Share

Easier to type, but getting the same error "The name 'Character$$anonymous$$ovement' does not denote a valid type ('not found')"

Would it help to say that the Character$$anonymous$$ovement script is C#?

avatar image
0

Answer by Shgoedt · Mar 06, 2013 at 01:30 PM

Check if your scripts are actually attached, then double-check the naming, capital letters included.

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html this is an example you could use.

But by the looks of it, it should work.

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 Walodcave · Mar 06, 2013 at 01:53 PM 0
Share

Just tried that, but seems like this is also giving me the same error. "The name 'Character$$anonymous$$ovement' does not denote a valid type ('not found')"

The script is attached and the spelling/capitalization seems correct.

Still causing problems.

avatar image
0

Answer by PAEvenson · Mar 06, 2013 at 01:11 PM

is this unityScript or c#?

there are a couple of ways to do this:

 charControl = GameObject.Find("player").GetComponent<CharacterMovement>();
 
 charControl.moveSpeed = 10;
 
 or
 
 charControl = GameObject.Find("player").GetComponent(CharacterMovement) as CharacterMovement;
 
 charControl.moveSpeed = 10;
 
 
 
Comment
Add comment · Show 5 · 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 Walodcave · Mar 06, 2013 at 01:27 PM 0
Share

It's in Java Script. I tried the code but now it gives me another error, "The name 'Character$$anonymous$$ovement' does not denote a valid type ('not found')"

avatar image PAEvenson · Mar 06, 2013 at 01:31 PM 0
Share

try:

 charControl = GameObject.Find("player").GetComponent("Character$$anonymous$$ovement") as Character$$anonymous$$ovement;
  
 charControl.moveSpeed = 10;
avatar image Walodcave · Mar 06, 2013 at 01:45 PM 0
Share

Same error. :(

avatar image PAEvenson · Mar 06, 2013 at 01:51 PM 0
Share

Like Shgoedt said, check your spelling. Also, do you have the script located in a special folder perhaps? Some folders get compiled at different times and if you are trying to access a script that hasnt been compiled yet, you will get an error.

Whttp://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.htmlW

avatar image Walodcave · Mar 06, 2013 at 02:09 PM 0
Share

Yes, they were in different folders. I changed it now but it still gives the same error. I checked the spelling and capitalization but they seem fine.

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

13 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

Related Questions

Multiple Cars not working 1 Answer

Public variable in script different for every game object. 1 Answer

Decreasing a value as another value increases through script 4 Answers

Get Value/String From GUIText 3 Answers

Getting error: Cannot modify a value type return value of `UnityEngine.Rigidbody.velocity'. Consider storing the value in a temporary variable 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