0
votes

I have a Parent GameObject ZombieArmy with an attached script Zombie; its Transform changes each time a new zombie is instantiated as a child. How do I prevent the zombieArmy transform from changing and keep its transformed fixed at Vector3(0,0,0) while having the zombie have its own unique transform from each reSpawn()?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Zombie : MonoBehaviour {

    public GameObject zombiePrefab;
    public Transform zombieArmy;
    public Transform zombieSpawnPoint;
    private Transform[] spawnPositions;
    public bool reSpawn = false;
    private bool lastToggle = false;
    private GameObject spawn;

    // Use this for initialization
    void Start () {
        spawnPositions = zombieSpawnPoint.GetComponentsInChildren<Transform>();
    }

    private void NewSpawn()   //spawn location of newZombie
    {
        if (reSpawn)
        {
            int i = Random.Range(1, spawnPositions.Length);
            transform.position = spawnPositions[i].transform.position;
            spawn = Instantiate(zombiePrefab, this.transform.position, this.transform.rotation, zombieArmy);
           // zombieArmy.transform.position = new Vector3(0, 0, 0);
        }
    }


    void Update () { //T-toggle
        if (reSpawn != lastToggle)
        {
            NewSpawn();
            reSpawn = false;
        }
        else
            lastToggle = reSpawn;
    }
}
1
From your code Transform zombieArmy is used only in Instantiate() and that is guaranteed not to change the Transform zombieArmy. That means Transform zombieArmy is changed elsewhere not in this code, and you should look up or provide other related classes to solve this issueMatt
I also read your previous post and it seems you are having troubles with basic setups. I'd recommend you to go to look up Unity's Tutorials to learn some of the best practices in Unity. Also it will be helpful if you look up more for designing classes and their roles. Questions like "Do zombies need to know the spawner's positions?" are what should be considered before codes are written. Google SOLID principles and other famous OOP design concepts as they will help you develop great games.Matt
This code looks fine. there must be something wrong elsewhere. find out where else accesses zombie army's transform.Bizhan
Thanks guys! After realizing from your comments that it wasn't a coding error as zombieArmy was called only in this script. I put zombieArmy GameObject into another GameObject and made that GameObject the Parent, and it worked! :DBenSmith

1 Answers

0
votes

If I'm understanding you correctly, that Zombie script is attached to the parent gameobject, right? Then your NewSpawn() method is a bit incorrect.. This line

    transform.position = spawnPositions[i].transform.position;

is actually changing the transform of the parent gameobject, as you say, because that's exactly what you're telling it to do. If what you want is place each newly spawned object in the location of the randomly selected spawn point, try this instead:

    int i = Random.Range(0, spawnPositions.Length);    // Any reason the 0th index shouldn't be used?

    spawn = Instantiate(zombiePrefab, this.transform);
    spawn.transform.position = spawnPositions[i].transform.position;
    spawn.transform.rotation = spawnPositions[i].transform.rotation;