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 Oninji · Oct 06, 2010 at 06:36 PM · javascriptgameobject

Activate/Deactivate Script from another Object.

Hello,

I'm trying to activate and deactivate a script that is on another gameObject.

To situate you, I have a script on my Character that when it meet some conditions it must deactivate or activate a specific script on the Main Camera.

Thank You.

Adding the scripts

This is CameraActivate situated on the Character.

var Cspeed = 2; var Camera : GameObject;

function Update () { var controller : CharacterController = GetComponent(CharacterController); var horizontalVelocity : Vector3 = controller.velocity; horizontalVelocity = Vector3(controller.velocity.x, 0, controller.velocity.z);

// The speed on the x-z plane ignoring any speed var horizontalSpeed : float = horizontalVelocity.magnitude; // The speed from gravity or jumping var verticalSpeed : float = controller.velocity.y; // The overall speed var overallSpeed : float = controller.velocity.magnitude;

 if (overallSpeed > Cspeed){
 Camera.GetComponent(CameraFocus).enabled = false;

 }

}

this is the CameraFocus situated on Main Camera that need to be controlled through CameraActivate

var target : Transform; var Distance = -50; var xsmoothTime = 0.5; var ysmoothTime = 0.5;

private var xVelocity = 0.0; private var yVelocity = 0.0;

function Update () { var newX : float = Mathf.SmoothDamp(transform.position.x, target.position.x, xVelocity, xsmoothTime); var newY : float = Mathf.SmoothDamp(transform.position.y, target.position.y, yVelocity, ysmoothTime); transform.position = Vector3 (newX, newY, Distance);

}

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

3 Replies

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

Answer by Loius · Oct 06, 2010 at 06:46 PM

UnityScript:

otherObject.GetComponent(NameOfScript).enabled = false;

C#

otherObject.GetComponent<NameOfScript>().enabled = false;
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 Oninji · Oct 06, 2010 at 07:01 PM 0
Share

Doesn't seem to work, dunno why. =\

avatar image PandoWooSetti Oninji · Jul 21, 2016 at 08:04 PM 0
Share

Because you have to Declair the Object it's self, add this to the top.

 public GameObject otherObject;
 
 <other coder>
 otherObject.GetComponent<NameOfScript>().enabled = false;


avatar image Oninji · Oct 06, 2010 at 07:38 PM 0
Share

Nvm it worked, it was my lack of attention that made an error.

avatar image oscarkool · Jan 09, 2014 at 03:59 AM 2
Share
 gameObject.GetComponent<Script>().enabled = true;

doesnt work for me either... Been scratching my head at this for the past couple of hours. It's written perfectly in the code, no compile errors, but in game doesn't work.

avatar image
3

Answer by okokok · Jan 14, 2013 at 07:32 PM

 // JS
 gameObject.GetComponent( Script ).enabled = true;
 
 // C#
 gameObject.GetComponent<Script>().enabled = true;
 // or
 (gameObject.GetComponent( typeof(Script) ) as Script).enabled = true;
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 aSaDaliii · Apr 01, 2015 at 06:49 AM 0
Share

thanks, it save my time work in JS

 gameObject.GetComponent( Temp ).enabled = true;

is it work in C#?

avatar image
0

Answer by humam · Aug 28, 2013 at 02:02 PM

 (GetComponent(enemyscript) as enemyscript).enabled = true;
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

6 People are following this question.

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

Related Questions

Quest Script Help 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Array problem -3 Answers

Accessing Script From Other Script Causes Lag? 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