- Home /
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);
}
Answer by Loius · Oct 06, 2010 at 06:46 PM
UnityScript:
otherObject.GetComponent(NameOfScript).enabled = false;
C#
otherObject.GetComponent<NameOfScript>().enabled = false;
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;
Nvm it worked, it was my lack of attention that made an error.
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.
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;
thanks, it save my time work in JS
gameObject.GetComponent( Temp ).enabled = true;
is it work in C#?
Answer by humam · Aug 28, 2013 at 02:02 PM
(GetComponent(enemyscript) as enemyscript).enabled = true;
Your answer
Follow this Question
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