x


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);  

}
more ▼

asked Oct 06 '10 at 06:36 PM

Oninji gravatar image

Oninji
332 43 47 50

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

UnityScript:

otherObject.GetComponent(NameOfScript).enabled = false;

C#

otherObject.GetComponent<NameOfScript>().enabled = false;
more ▼

answered Oct 06 '10 at 06:46 PM

Loius gravatar image

Loius
10.9k 1 12 43

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

Oct 06 '10 at 07:01 PM Oninji

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

Oct 06 '10 at 07:38 PM Oninji
(comments are locked)
10|3000 characters needed characters left
// JS
gameObject.GetComponent( Script ).enabled = true;

// C#
gameObject.GetComponent<Script>().enabled = true;
// or
(gameObject.GetComponent( typeof(Script) ) as Script).enabled = true;
more ▼

answered Jan 14 at 07:32 PM

okokok gravatar image

okokok
16 1 2

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5270
x3568
x3418
x2170

asked: Oct 06 '10 at 06:36 PM

Seen: 6693 times

Last Updated: Jan 14 at 07:32 PM