- Home /
Taking value of variable from a script of Gameobject to a script attached to other Gameobject
I am developing a game, actually I have done hundreds of lines of coding now I stuck in the simple problem. I have a script attached to an empty GameObject and this script is doing something like getting value of two variables dynamically from a script attached to another GameObject in order to switch from Player 1 to Player 2. Actually it is multiplayer game.
Now what I am doing here is..
public var script : MainScript;
public var playerPrefab : GameObject;
function Awake(){
script = playerPrefab.GetComponent(MainScript);
}
function update()
{
// here I am accessing two variables available in MainScript
//But it is showing wrong answer.
//I am doing something like shown below:
Debug.Log(script.variable1); // variable1 is boolean
// even If I change variable1 dynamicaly in "Mainscript"
//script.variable1 is not changing.
}
Those variables have "public" access modifier in MainScript. I do not understand what to do now. Please help me.
Your code looks fine. Are you sure 'playerPrefab' is initialized to an instantiated game object? If it refers to the actual prefab, the value will not change if you are changing the value on an instantiated copy.
Your answer
Follow this Question
Related Questions
Can't understand generic getComponent for Js 1 Answer
Use variable from another script with WWW give NULL? 1 Answer
Changing variables in another script 3 Answers
How to set a variable equal to another variable in another gameobject? 2 Answers
Why can't I change another script's variable with this script? Thanks, 1 Answer