S
S
Suren2018-03-12 01:43:37
C++ / C#
Suren, 2018-03-12 01:43:37

Instantiate why does the prefab change?

The problem is that for some reason, Instantiate creates a prefab (my prefab is a built map, with a lot of objects), but it is created modified, I don't understand why.
Below is a screenshot 5aa5affcbc7c7845362935.jpegWhere you can see that there are coins, for example, I collected them. After I went through the collider, you will understand from the code through which one, the implementation of the method begins, where there is Instantiate, but for some reason, it creates a map object, changed, i.e. those coins that I collected, he actually creates without them. prefabricated 5aa5b0da2c1b1223707751.jpeg
Here is the code itself

public class SpaunZone : MonoBehaviour
{
    public bool spaun = false;
    public GameObject[] pref;
    public int indexNumber;
    public GameObject spaunPoint;
    // Use this for initialization
    void Start ()
    {
        spaun = false;
  }

    private void Update()
    {
        Spaun();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.transform.tag == "Player")
        {
            spaun = true;
        }

    }

    private void Spaun()
    {
        if (spaun)
        {
            spaunPoint = GameObject.Find("SpaunZonePref");
            Instantiate(pref[indexNumber], spaunPoint.transform.position, Quaternion.identity);
            spaun = false;
            Destroy(pref[indexNumber], 2);
        }
        else
        {
            spaunPoint = null;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Gaydak, 2018-03-12
Jagaryan @saylar

And please show in the inspector what you have links to in public GameObject[] pref;
maybe you have a link not to a prefab from the PROJECT, but a link to a PREFAB IN THE SCENE. then you will not be taken from, so to speak, resources, but a copy of the object that exists and changes in the scene will be created ..
UPD
I will bring it here so that people do not read all the comments.
In general, the point is that the prefab contained a link to ITSELF. and after instantiation - began to refer to itself in the SCENE.
it is necessary to separate the storage of links into prefabs, and the prefabs themselves.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question