fluentparrot
fluentparrot

Reputation: 29

make speed down for prefab

public class a : MonoBehaviour 
{
     private float speed;

     void Start()
     {
          speed=1;
     }

     void Update()
     {
         this.transform.Translate (Vector2.right * speed * Time.deltaTime);
         animator.Play ("gub");
      }

      public void button  ()
      {
           speed = 6f;
      }
}

When I press button , I'd like to change the objects speed which are prefab. The first prefab's speed changes... but the rest do not change.

What is wrong?

Upvotes: 0

Views: 46

Answers (1)

Gabriel Capeletti
Gabriel Capeletti

Reputation: 106

You are changing a variable of an instance of a GameObject, not a prefab.

A prefab is like a blueprint of a GameObject, so most of the data will be copied to each instance of that GameObject. But all the code that you write is referent to each instance, in that way each object will move freely from one to another, imagine if every time that you made a change in a GameObject variable, like its life, all the other instances changed the life together, it would be a mess.

To solve your problem you will need to get the information from that variable from a common place, something like a LevelManager, a place were every instance of the object can get the same value, it can be a static field in some object or a field in a Singleton, that depends on the structure of your game.

Upvotes: 1

Related Questions