• 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Zaffer · Apr 22, 2012 at 10:11 PM · referencecharacter controller

how to reference character controller by script from another object

Hi,

I am trying to reference the character controller from a script attached to another object (for example, from a script attached to a cube). It is easy to just leave the "controller" var public and drag the character controller on to it in the inspector, but I would rather reference it entirely by code -- something like below, which does not work. Any suggestions? Thanks. Zaffer

  private var controller : CharacterController;
  function Start(){
     controller = gameObject.GetComponent(CharacterController);
     Debug.Log(controller);
  }

P.S. I seem to be having trouble formatting my code -- using the "pre" and "code" tags which don't seem to work very well. Any suggestions? Thanks.

Comment
Add comment · Show 7
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 Lo0NuhtiK · Apr 22, 2012 at 10:17 PM 2
Share

lol I just reformatted it for you a $$anonymous$$ute ago, and now you put it back messed up again :D

avatar image Lo0NuhtiK · Apr 22, 2012 at 10:21 PM 0
Share

There :P reformatted again... When making code-blocks on here, you have to make sure each line inside the code block area has at least 3 spaces at the start of the line before you start typing.

avatar image Lo0NuhtiK · Apr 22, 2012 at 10:21 PM 0
Share

damn it.. you did it again lmfao Edit Good to see you're at least trying to get it formatted right though. 90% of the time people just leave it the way it was the first time they posted it.

avatar image Zaffer · Apr 22, 2012 at 10:26 PM 0
Share

Sorry Lo0Nuhti$$anonymous$$,

If you want to try again, I promise I won't touch it.

avatar image Lo0NuhtiK · Apr 22, 2012 at 10:27 PM 0
Share

lol alright

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lo0NuhtiK · Apr 22, 2012 at 10:42 PM

I haven't messed with character controller much at all, so there might be an even easier way of accessing that than this ; but as for accessing other scripts on a specific object you could go about it with something like this->

 var player : GameObject ;
 var script : ScriptName ;
  
 function Start(){
    player = gameObject.FindWithTag("Player") ;
    script = player.GetComponent(ScriptName) as ScriptName ;
 }
  
 function Update(){
    if(Input.GetKeyDown(KeyCode.T))
       script.FunctionName() ;
       script.variableName = whateverValue ;
    }
 }
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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Creating a pointer variable to a GameObject inside a class that does not extend MonoBehavior? [C#] 2 Answers

Setting up reference to an inactive GameObject through script 1 Answer

Object reference not set to an instance of an object 1 Answer

Is it possible to have multiple stencil reference values in one shader? 0 Answers

Dictionary.TryGetValue for js 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