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 srafciger · Nov 06, 2011 at 04:42 PM · getcomponentreference

Script Referencing

Could someone explain me how do I use one scripts elements in other scripts (GetComponent), and also help me with following script ?

Here's the script cameraSwitcher.js :

 var MainCamera : Camera;    
 var FPCamera : Camera;    
 var Active : boolean = false;
 
 function Start(){    
    MainCamera.enabled = true;
    FPCamera.enabled = false;
 }
     
 function Update(){    
 if(Input.GetKeyDown("tab") && Active == false)    
 {    
    MainCamera.enabled = false;    
    FPCamera.enabled = true;    
    Active = true;    
 }    
 else if(Input.GetKeyDown("tab") && Active == true)   
 {    
    MainCamera.enabled = true;    
    FPCamera.enabled = false;    
    Active = false;
 }
 }




I want to create second script that would control whether the gameObject I choose is visible or not.

So, if MainCamera is enabled gameObject.renderer is enabled too.

And, if MainCamera is disabled gameObject.renderer is also disabled.

I just don't understand how to do it I looked for scrpiting reference on Unity's site but I still can't quite figure it out.

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

2 Replies

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

Answer by aldonaletto · Nov 06, 2011 at 05:01 PM

Firstly, you could simplify the Update function of cameraSwitcher.js this way:

function Update(){    
  if (Input.GetKeyDown("tab"))    
  {
    Active = !Active;    
    MainCamera.enabled = !Active;    
    FPCamera.enabled = Active;    
  }    
}
You have two ways to access Active from other scripts:

1- If there exists one only cameraSwitcher instance in your program, you can declare the variable Active as static in cameraSwitcher.js:
 static var Active: boolean = false;

then access it in other scripts like this:

     if (cameraSwitcher.Active){

2- If you have other instances of cameraSwitcher, you must have some reference (transform, collider, gameobject etc.) to the object whose Active variable you're interested in. You can have a Transform variable in the other script and assign the object in the Inspector:

other script:

 var camSwitcherObj: Transform; // drag the object here
 var camSwitcher: cameraSwitcher; // this will contain the script reference

 function Start(){ // get a script reference at Start to save time
     camSwitcher = camSwitcherObj.GetComponent(cameraSwitcher);
 }

 // then you can use camSwitcher as the script reference:
     if (camSwitcher.Active){



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 srafciger · Nov 08, 2011 at 06:59 PM 0
Share

I figured out i could do this without any script reference, but this was useful in my other script

avatar image
0

Answer by Fabkins · Nov 06, 2011 at 04:49 PM

You can use GameObject.Find("object_name") to get a reference to the object.

 // object_name is the name you've given the object in the inspector
 myobject=GameObject.Find("object_name");  

You can then do whatever you like to it...

  myobject.transform.y += 10; // moves it up 10
  myobject.active=false;  // makes it inactive
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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Reference Not Set toInstance of Object 1 Answer

Can I access a script referenced in another script (without getcomponent)? 5 Answers

Is performance related to the script size that is called by GetComponent? 1 Answer

Can Only Reference My Own Scripts 1 Answer

Accesing script from newly instantiated game object 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