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
1
Question by p_unity · Jun 06, 2013 at 11:46 AM · componentaccessingmotorcharacter-controller

accessing components of character motor script

i have pick ups that increase the players speed for a period of time. I'm using built in FPS character controller, and i can access it but i cant declare it in a start function, i have to GetComponent each time i want to adjust it, i dont know why?

 function OnControllerColliderHit (pickUp : ControllerColliderHit){
 
     if (pickUp.collider.gameObject.tag == "GoldenSkull")
     {
         Destroy (pickUp.gameObject);
         GetComponent(CharacterMotor).movement.maxForwardSpeed = 25;
         
         yield WaitForSeconds (4.0);
         GetComponent(CharacterMotor).movement.maxForwardSpeed = 6;
         
     }
 
 }

this works...

 var motor : CharacterMotor ;
 var speed : float ;
 
 function Start (){
 
     motor = GetComponent(CharacterMotor);
     speed = motor.movement.maxForwardSpeed;
 }
 
 function OnControllerColliderHit (pickUp : ControllerColliderHit){
 
     if (pickUp.collider.gameObject.tag == "GoldenSkull")
     {
         Destroy (pickUp.gameObject);
         speed = 20 ;
         yield WaitForSeconds (4.0);
         speed = 6 ;
     }
 
 }

but this will not? there is no errors but the speed is not adjusted? why cant i declare the speed as a variable and just access it like that?

Comment
Add comment · Show 2
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 robertbu · Jun 06, 2013 at 02:41 PM 0
Share

@Jaxcap's solution will work just fine, but you can go one class further:

 motor = GetComponent(Character$$anonymous$$otor);
 movement = motor.movement;
 
 movement.maxForwardSpeed = 20.0;

You need to figure when you are accessing something by reference vs. by value. Classes are by reference. Primitives like floats are by value. Anything in the reference manual defined as a 'struct' is by value (like Vector3).

avatar image p_unity · Jun 07, 2013 at 06:58 AM 0
Share

i see, i see now. very helpful

1 Reply

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

Answer by Jaxcap · Jun 06, 2013 at 02:19 PM

In the second one, when you do:

 speed = motor.movement.maxForwardSpeed;

since they are both primitives, by doing that you're not actually "linking" the two variables together. You are just setting the value of speed equal to the value of motor.movement.maxForwardSpeed. So when you change the value of speed in the OnControllerColliderHit function, it's only changing that value, not the one in the motor object.

With what you have, you could do:

 var motor : CharacterMotor ;
  
 function Start (){
  
     motor = GetComponent(CharacterMotor);
 }
  
 function OnControllerColliderHit (pickUp : ControllerColliderHit){
  
     if (pickUp.collider.gameObject.tag == "GoldenSkull")
     {
        Destroy (pickUp.gameObject);
        motor.movement.maxForwardSpeed = 20 ;
        yield WaitForSeconds (4.0);
        motor.movement.maxForwardSpeed = 6 ;
     }
  
 }
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 p_unity · Jun 07, 2013 at 06:57 AM 0
Share

excellent. Great advice as well guys. Still a beginner really but ill keep all that in $$anonymous$$d when accessing components. thanks very much.

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

15 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

Related Questions

Difficulties accessing other components 4 Answers

Why GetComponentInChildren is not working ? 3 Answers

2D Animation does not start 1 Answer

differences: generic GetComponent. 3 Answers

Activate objects/crafting in a game. 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