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
3
Question by Dreeka · Jan 23, 2012 at 03:20 PM · variableassign

assign a script to a variable

Hey,

How can I assign a script to a variable?

For example, I have a controls.js, which is attached to a game object. I also have an other script, manager.js

In manager.js how can I do the follwing:

 var controlScript : ??;
 var object        : GameObject;
 
 function Start()
 {
      controlScript = object.GetComponent("controls");
 }


Thanks for your time!

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

Answer by syclamoth · Jan 23, 2012 at 03:27 PM

Well, in UnityScript, the filename is also the typename!

So, if you have a file called controls.js, you can define a variable of that type with

 var controlScript : controls;

It's similar to how you are using GetComponent, later- the typename in quotes that you specified in the string paramater is the same as the one you would use when declaring the variable.

Comment
Add comment · Show 6 · 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 Dreeka · Jan 23, 2012 at 03:28 PM 0
Share

Thanks! :)

avatar image syclamoth · Jan 23, 2012 at 03:28 PM 1
Share

(In C# you have to make sure you set the typename to be the same as the filename manually, because although it compiles properly, Unity gets angry if you try to attach it to an object if it's wrong!)

avatar image KUFgoddess syclamoth · Jan 30, 2017 at 01:06 AM 0
Share

could you show us an example in C#?

avatar image $$anonymous$$ · Jul 08, 2015 at 09:41 PM 1
Share

What if the script you are trying to assign to the variable is a C# script?

avatar image KUFgoddess $$anonymous$$ · Jan 30, 2017 at 01:06 AM 0
Share

did you ever find an answer to that question? I need to know as well.

avatar image IvovdMarel KUFgoddess · Jan 30, 2017 at 01:28 AM 0
Share

Have you considered googling it? No worries, I did it for you.

http://www.41post.com/1935/program$$anonymous$$g/unity3d-js-cs-or-cs-js-access

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there a way to make a variable correspond to multiple different scripts? 1 Answer

Enemy Health Problems 1 Answer

Changing variable from another script 1 Answer

How to modify variables from scripts in standard package? 2 Answers

UnassignedReferenceException: The variable enemy of EnemiesSpawner has not been assigned unity3d 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