- Home /
Setting a variable to a variable from another script
Im trying to set a game object variable to the same game object variable from another script.
so script one has:
var object : GameObject;
and script two has:
var target : GameObject;
how do I set script two's target to script one's object using GetComponent?
Answer by Kleptomaniac · Apr 08, 2012 at 07:20 AM
Here you go!
var target : GameObject;
var script1Obj : GameObject;
private var script1 : Script1;
function Start () {
script1 = script1Obj.GetComponent(Script1);
target = script1.object;
}
Set script1Obj to the gameObject on which Script1 is attached, and set make sure to change all references to 'Script1' within the script to whatever the actual name of that script is.
Hope that helps, Klep
Your answer
Follow this Question
Related Questions
Getting errors when referencing a variable. 1 Answer
fix script to get C# var from another object instead of object this script is attached to 2 Answers
how to access a float from one script in other script in different objects 3 Answers
How would I change the value of one prefab's variable via script without changing all of them? 1 Answer
How to Access Components in Children of GameObject? 1 Answer