D
D
Deathgar2017-04-14 22:38:08
C++ / C#
Deathgar, 2017-04-14 22:38:08

Will the script added to the prefab be common to all objects created by this prefab?

4e79449b915a497fb395e36e45b6164d.png
Green - prefab, Red - objects created using the prefab.
The object itself, created using it:
ee4cb224e8da4c2dafb63e7424a3c8b7.png
Script code in the prefab:

using UnityEngine;
using System.Collections;

public class UpWall : MonoBehaviour {

  public Transform camer;
  public camera cam;
  private Transform pointDistans;
  private float distans;
  public float distansSpawn = 12f;
  public float speedUp = 5f;
  private float count;
  public GameObject[] barriers;
  // Use this for initialization
  void Start () {
    pointDistans = GameObject.Find("point").GetComponent<Transform>();
    count = transform.position.y + 41f;
    int rand = (int)Mathf.Round(Random.Range (0, barriers.Length));
    Debug.Log (rand);
    GameObject inst = Instantiate (barriers[rand]);
    inst.transform.parent = transform;
    inst.transform.position = transform.position;
    distansSpawn += cam.speed / 5f;
  }
  
  // Update is called once per frame
 // Высчитывает расстояние от барьера до камеры при нужном расстоянии, подымает его.
  void FixedUpdate () {
    distans = Vector3.Distance (pointDistans.position, camer.position);

    //Debug.Log (distans);

    //Debug.Log (transform.position.y);
               
    if (distans <= distansSpawn) 
    {
      if(transform.position.y <= count)
      {
        transform.Translate(new Vector3(0,41f,0) * speedUp  * Time.deltaTime);
      }
    }
  }
}

The code (see everything in FixedUpdate) calculates the distance from the barrier to the camera at the required distance, raises it. As far as I understand, he must calculate the distance for each barrier, but he starts raising barriers when he comes to the required distance from the 3rd, although the first one has not yet risen, while everyone starts to rise at the same time.
Maybe I don't understand something. Help.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vopross, 2017-04-14
@Deathgar

I've only used the unit a couple of times, so I could be wrong, but for each prefab a copy of the script will be created => the scripts won't be shared.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question