- Home /
Question by
kefo · Nov 29, 2016 at 08:16 AM ·
gameobjectprefabvariable
How can i access in a prefab a variable from a gameObject?
Hi guys,
i instantiate a gameObject every few seconds. In the attached script i want to use a variable from a Gameobject (which is not a prefab). This means, the variables should be the same in every created clone.
How to Access the variable?
Script which is attached to prefab:
public GameController gameController;
void Update()
{
float variable = gameController.gameSpeed;
}
Script attached to gameObject:
public float gameSpeed;
void Update ()
{
gameSpeed = gameSpeed + 0.1f;
}
Normally i attach the Gameobject to the prefab in the Inspector, but it is not even possible. So what am i doing wrong here?
Comment
Answer by kefo · Nov 30, 2016 at 08:29 PM
found a solution finally, just had to add this Code to the prefab:
GameObject gameControllerObject = GameObject.Find ("GameController");
if (gameControllerObject != null)
{
gameController = gameControllerObject.GetComponent <GameController>();
}
i searched for the wrong gameObject, that was my big misttake^^