Instantiated object does not spawn directly on target

when my object weirdly stays below 200 units on the z axis when spawned outside of it, It just spawns in random places.

    public GameObject friendlyUnit;
    public int enemyLimit = 3;
    public int friendlyLimit = 3;
    public float spawnTimerLimit = 5f;
    public bool ControlPointable;
    public GameObject controlpoint;
    public bool Areabound_Allow;
    public GameObject areaBound;
    AreaBound areabound_Script;
    ControlPoint controlpointscript_reference;

    private float spawnCount = 5f;

    private List<GameObject> spawnedEnemies = new List<GameObject>();

    void Start()
    {
        if (Areabound_Allow == true)
        {
            areabound_Script = areaBound.GetComponent<AreaBound> ();
        }
        if (ControlPointable == true)
        {
            controlpointscript_reference = controlpoint.GetComponent<ControlPoint> ();
        }
    }
    // Use this for initialization
    void Update () {
        spawnCount -= Time.deltaTime;
        foreach(GameObject target in spawnedEnemies)
        {
            if(target == null)
            {
                spawnedEnemies.Remove(target);
                Destroy(target);
            }
        }
        if(spawnCount <= 0f)
        {
            spawnCount += spawnTimerLimit;
            tryToSpawn();
        }
    }

    // Update is called once per frame
    private void tryToSpawn()
    {
        if(ControlPointable == true)
        {
            if(controlpointscript_reference.Allegiance == "Green")
                return;
        }
        if (Areabound_Allow == true)
        {
            if(areabound_Script.friendlies.Count == 0)
                return;
        }
        if (spawnedEnemies.Count < enemyLimit)
        {
            GameObject newEnemy = (GameObject)Instantiate(enemyUnit);
            newEnemy.transform.position = transform.position;
            spawnedEnemies.Add(newEnemy);
        }
    }
}

Hi DukinglyNukem!

If I understand it, your problem is that the instance is not in the correct position. After a quick view to your code this could be because this line of code:

“newEnemy.transform.position = transform.position

“transform.position” gives to your instance the position where the script is atached (I don´t know if this is what you wanted). And you are giving to “newEnemy” a global position (be careful to know if you want “transform.position” or “transform.localposition”)

Tell me if this helps you to solve your problem, if not, I will take a deep look into your code. Also I recommend you to use public Transform variables to positionate element instances, that lets you have more control about where you instantiate an element.

Waiting for you answer. =)

looks to me like youre not specifying where to instantiate.

public GameObject Marker;
public Vector3 spawnMarker;

spawnMarker = Marker.transform.position;
spawnHere = new Vector3(spawnMarker);

GameObject newEnemy = (GameObject)Instantiate(enemyUnit, spawnHere);

off the top of my head but
i think thatll work

wherever you put the Marker GO, it will instantiate the enemy there