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 deadpenguin · Jan 25, 2014 at 07:55 PM · javascriptvariablegetcomponentcharactermotor

Accessing a variable that has a unique class

this is a portion of my animation code:

     function Start () {
     var motor : CharacterMotor = gameObject.GetComponent(CharacterMotor);
     animation["idle"].wrapMode = WrapMode.Loop;
     animation["jump_pose"].wrapMode = WrapMode.ClampForever;
     animation["walk"].wrapMode = WrapMode.Loop;
     animation["run"].wrapMode = WrapMode.Loop;
     }
 
     function LateUpdate () {
         charVelocity = motor.movement.velocity;
         CalculateState();
         ApplyState(); 
     }

My problem is in the line "charVelocity = motor.movement.velocity;" it says Unknown identifier 'motor' (BCE0005).

Movement is a variable with a class of its own and I am trying to access the velocity part of it after the code has run in the other script.

Comment
Add comment · Show 3
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 getyour411 · Jan 25, 2014 at 07:56 PM 0
Share

Try this

  var motor : Character$$anonymous$$otor = gameObject.GetComponent<Character$$anonymous$$otor>();

I use C# but I think JS uses that; basically the problem is that motor isn't getting the reference it should. I'm assu$$anonymous$$g the Character$$anonymous$$otor component is on this same gameobject.

You could also just declare motor as public and drag/drop the Character$$anonymous$$otor object into it in the Editor.

avatar image Benproductions1 · Jan 25, 2014 at 11:50 PM 1
Share

@getyour411 In UnityScript generics are actually done using a dot:

 GetComponent.<Character$$anonymous$$otor>()

But it's actually suggested to not use the generic GetComponent method in UnityScript and rely on the one the OP is using. There's nothing wrong with the getting of the Character$$anonymous$$otor component.

avatar image Josh707 · Jan 25, 2014 at 11:55 PM 0
Share

'motor' is local to whatever it was declared in, if you create the variable inside of a function it is local to that function and can't be accessed outside of it. You'll have to store the data in a variable outside of any functions of type Character$$anonymous$$otor/Component.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by oatsbarley · Jan 25, 2014 at 11:56 PM

You declare motor in Start() and then reference it inside LateUpdate() which is outside of its scope, hence the "Unknown Identifier" error - it doesn't exist there. You need to declare motor as a class variable, so that all methods of the class can access it.

Comment
Add comment · Show 2 · 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 oatsbarley · Jan 25, 2014 at 11:57 PM 0
Share

@Josh707 ninja'd me in the question comments.

avatar image Josh707 · Jan 26, 2014 at 12:05 AM 0
Share

Hehe :P ­­

avatar image
1

Answer by Benproductions1 · Jan 25, 2014 at 11:57 PM

Your problem is, that you're trying to access a variable that doesn't exist. On line 2 in Start you declare a variable motor of type CharacterMotor. Since the variable is declared inside of a function, it will only exist during that function and will be freed once the function returns. It doesn't exist anymore when you try to access it in the Update function.
To fix this, you should make the motor variable a private variable on your MonoBehaviour class. Class variables (fields) exist as long as the object they are part of does.

 class SCRIPTNAME extends MonoBehaviour {
     private var motor:CharacterMotor;
     
     function Awake() {
         #This is the fastest method of GetComponent
         motor = GetComponent(typeof(CharacterMotor)) as CharacterMotor;
     }
     
     function Update() {
         #Yay, it exists here too!!
         Debug.Log(motor);
     }
 }
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 Eric5h5 · Jan 26, 2014 at 12:06 AM 0
Share

Scripts in Unityscript are already classes that extend $$anonymous$$onoBehaviour. :)

avatar image Benproductions1 · Jan 26, 2014 at 12:09 AM 0
Share

But they don't have to be. And they can contain other classes, so in order to remove confusion and to stay consistant it's better to declare the class explicitly. At least thats my opinion on the matter. It also re$$anonymous$$ds new people, that they are really just writing classes, not some magical script thing that works as a $$anonymous$$onoBehaviour magically...

avatar image Eric5h5 · Jan 26, 2014 at 01:04 AM 0
Share

They actually do have to be; every script is a class that extends $$anonymous$$onoBehaviour. You can put other classes in there that don't, but you really are writing a magical script thing that extends $$anonymous$$onoBehaviour since that's how Unityscript works.

avatar image Benproductions1 · Jan 26, 2014 at 01:55 AM 0
Share

If you write a class in the script with the same name as the file it's in, it will replace the magical crap that the UnityScript translator adds in. (Yes, I've done it many times before)

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

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

Issues with trying to change a variable in another script 1 Answer

How could you access a script of varying name? 5 Answers

CharacterMotor problem (Js) 1 Answer

Need to use 2 different language scripts. 1 Answer

Static Variable Problem 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