x


How to activate another script?

Hello, this is a basic question, but if I want to have a trigger (button, collision, etc.) to activate another script, how would I do that? How would I limit the amount of time the script would be activate? An example would be: The player hits a button and a "constant force" script is activated for 5 seconds (to simulate nitrous oxide in a car). Thanks.

more ▼

asked Nov 05 '10 at 03:57 AM

Tyler 2 gravatar image

Tyler 2
1.1k 212 247 264

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

1 answer: sort voted first

Would it be something like this you are looking for?

function Update ()
{
    if (Input.GetKey(KeyCode.Space) && nosReady)
        NOS();
}

function NOS()
{
    nosReady = false;
    nosActive = true;
    yield WaitForSeconds(5);
    nosActive = false;
    yield WaitForSeconds(5);
    nosReady=true;
}

function FixedUpdate()
{
    if (nosActive)
    {
        //Add force
    }
}
more ▼

answered Nov 05 '10 at 04:05 AM

Atnas1010 gravatar image

Atnas1010
1.1k 6 10 26

While a NOS type boost was one of the things I had in mind, I meant triggering a completely separate script that is in the scene. Also, I am new to unity, so I dont even know what to do with your script (how would I make it work)?

Nov 05 '10 at 04:26 AM Tyler 2

You would use gameObject.AddComponent(TheNameOfTheScript); But if you are that new to unity, I would suggest poking around in the http://unity3d.com/support/documentation/ScriptReference/

Nov 05 '10 at 04:33 AM Atnas1010
(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:

x5275
x1019
x353
x71

asked: Nov 05 '10 at 03:57 AM

Seen: 2059 times

Last Updated: Nov 05 '10 at 03:57 AM