Home > OS >  Instatiated objects from the same prefab share a position?
Instatiated objects from the same prefab share a position?

Time:03-01

I'm starting a simple JRPG: generating a party from a group of prefabs, then moving each into position by changing their transform. But seeing a strange one where 3 goblins spawned from the same prefab are moving to exactly the same position. The other objects instantiated from different objects are moving correctly.

In the player, I can move the goblins by changing their transform, and they are separate. However the prefab itself is changing position to match the 'last' goblin spawned

Any hints? Am I somehow instantiating them to a common object?

Objects created and positioned in PartyManager:

public bool playerParty = true;
public GameObject[] party=new GameObject[4]; 

// Start is called before the first frame update
void Start()
{
    for (int i=0; i<party.Length; i  ){
        GameObject character = Instantiate(party[i],this.gameObject.transform);
    }
    positionCharacters();
}


void positionCharacters(){
    float facing = -1.0f;
    if(playerParty) facing=1.0f;
    
    for (int i=0; i<party.Length; i  ){
        party[i].transform.localPosition = new Vector3(-0.3f*i*facing, -0.05f*i,-0.1f*i);
        print(party[i] " moved to " party[i].transform.localPosition);
    }
    
}

enter image description here

CodePudding user response:

First of all you don't seem to be storing references to the instantiated characters, GameObject character just goes out of scope immediately. If you put three references to the same object into GameObject[] party you shouldn't be surprised only the last set position is used.

  • Related