problem with prefab's script
I know for sure that this question has been answered before but because in each answer the solution presented was adjusted to someone's classes, variables, etc it was difficult for me , who just begun to work in unity, to understand it.
My problem through a very general example : I create a cube in the Hierarchy using the create button. Then I drag the cube from the hierarchy to the project tab (so im creating its prefab i guess). Then i add the following js script, named Cube_script, to both "hierarchy cube" and "prefab cube":
#pragma strict
public var variable = 10000000;
function Start () {
}
function Update () {
variable = variable - 1;
}
Then i hit play and i observe from the inspector that that the variable from the "hierarchy cube" is decreasing but the variable from the "prefab cube" is staying the same. All i want to do is to force the variable of the "prefab cube" to "follow" the variable of the "hierarchy cube".
Answer by ralphlizard · May 19, 2016 at 05:13 PM
It sounds like you have the "hierarchy cube" in the hierarchy while the "prefab cube" is not. The prefab is not being updated because it is not meant to -- it is simply a reference that you can make instances of in the scene. Your "hierarchy cube" is one such instance. If you want a variable in the prefab to be the same across all instances, you can try making that variable static, like this:
static var variable = 10000000;
function Start () {
}
function Update () {
variable -= 1;
}
That didn't work for me. If you want me to elaborate :
I have a second script written in C# and from this script i want to control variable
.So i create a GameObject in the C# script and i dragged in the inspector the "prefab cube" because i saw that i can't drag there the "hierarchy cube". And the problem is that the variable
of "prefab cube" doesn't change and therefore i can't control it.
So making variable static gave me an error "Static member........cannot be accessed with an instance reference, qualify it with a type name ins$$anonymous$$d"
What do you mean you "create[d] a GameObject in the C# script"? Do you mean you instantiated a GameObject?
You should not be able to drag any GameObject into the inspector. Do you mean you tried to set a SerializeField (of what script in what GameObject?) in the inspector by dragging something into it?
Your answer
Follow this Question
Related Questions
Prefab with same scripts do the same thing 0 Answers
Instantiating prefab causes existing instances to change values 1 Answer
How to copy GameObject and preserve Prefab connection and Component values from editor script 2 Answers
Prefabs not instantiating after build 0 Answers
How To Generate Platforms Down Each other in a pattern with some different positions Unity 1 Answer