- Home /
How to Activate Script in another Object?
Pretty simple, hopefully, but I'm a complete disaster with scripting. I'm using a Lerp script that moves an object from Point A to Point B which is great. However, I'd like to enable that movement, via another script, only when another object has been clicked. I've attempted various solutions with no luck.
Here's the movement script (called Lerp2)
// Transforms to act as start and end markers for the journey.
var startMarker: Transform;
var endMarker: Transform;
// Movement speed in units/sec.
var speed = 1.0;
// Time when the movement started.
private var startTime: float;
// Total distance between the markers.
private var journeyLength: float;
var target : Transform;
var smooth = 5.0;
function Start() {
// Keep a note of the time the movement started.
startTime = Time.time;
// Calculate the journey length.
journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
}
// Follows the target position like with a spring
function Update () {
// Distance moved = time * speed.
var distCovered = (Time.time - startTime) * speed;
// Fraction of journey completed = current distance divided by total distance.
var fracJourney = distCovered / journeyLength;
// Set our position as a fraction of the distance between the markers.
transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
}
I'm assuming there's an easy way using OnMouseOver and GetMouseButtonDown to have a line of code that activates this script from another object.
Thanks in advance :)
Answer by djfatsteve2 · Apr 04, 2014 at 10:21 PM
to connect scripts...
(at top of file)
var scriptVar : exactNameOfScriptYouWishToAccess;
to call a function on that script from the one you included (as seen above)
scriptVar.otherScriptFunction();
Remember: You'll have to connect the scripts in the inspector by dragging the object that contains the specified script on it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613141939im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Another Java to C# Conversion Question!! 1 Answer
Disabling/Enabling Different Character Controllers. 1 Answer
disable/enable script js 2 Answers
Disable & Re-enable Script 1 Answer
enable disable variable of a script from another script 4 Answers