- Home /
How to pass a variable value from a script attached to a scene object to prefab?
Actually I was creating a Billiard game, I was calculating power from "gui texture" and then sending it to a script which was attached to cueball but both were in scene. Now I am creating it multiplayer so I have made cueball a prefab and removed it from scene. How can I send "power" value attached to an object in scene to a script attached to prefab which is in project?
I was using code earlier as shown below:
other = GameObject.Find("CueBall"); other.GetComponent("script").power = thePower*50;Now "thePower" is a variable of an object which is in scene but the "power" is a variable in project as it is attached to prefab(CueBall).
Answer by robertbu · Dec 19, 2013 at 03:50 PM
You cannot send anything to a prefab since a prefab does not exist in the scene. But if you are talking about a game object in the scene created from a prefab:
Javascript:
other = GameObject.Find("CueBall");
other.GetComponent(script).power = thePower*50;
C#:
other = GameObject.Find("CueBall");
other.GetComponent<script>().power = thePower*50;
It does not works with an instance of object. I am doing it.
What is 'not work' mean? Not compile? Generate a null reference exception? Are you sure that the name in the Find() is identical to the name on the game object in the scene? Are you sure that the 'CueBall' object exists at the time this code is executed?
it works when I don't use prefab. But when I use prefab then it show null reference.
Should I instantiate in Start() actually I am crating an instance of prefab in Update.
But it won't work in start() because I am using Network.Instantiate
Your answer
![](https://koobas.hobune.stream/wayback/20220613124508im_/https://answers.unity.com/themes/thub/images/avi.jpg)