I used same code like in Shooter script, there its working and here not.
When I am creating deffenders by click so in unity are added under all object I dont know why.
Can help me someone ? My object is called Deffenders.
private GameObject deffendersParent;
void Start(){
deffendersParent = GameObject.Find("Deffenders");
if (!deffendersParent)
{
deffendersParent = new GameObject("Deffenders");
}
}
Hi Adrian_Orcik,
In theory, what you have should generate the “parent” (read: empty) GameObject - Just double check in Play Mode.
SO, if you do have a “defendersParent” object to put the Defenders into, the next thing I’d check is that you’re actually setting the defender’s transform.parent to this “defendersParent” object.
This happens in the video at around about 20:00-ish.
Hope this helps.
P.S. It’s usually more helpful to include a full script - the error isn’t always where you think it is 
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DefenderSpawner : MonoBehaviour {
private GameObject defendersParent;
// Use this for initialization
void Start () {
GameObject.Find ("Defenders");
if (!defendersParent) {
defendersParent = new GameObject ("Defenders");
}
}
void OnMouseDown(){
if (UnitButton_Script.selectedDefender) {
// Instantiate<T> is a Generic Method that creates an Object of type T.
// This means we don't need to add "as GameObject" after the method.
Instantiate<GameObject> (
// This is the Defender/GameObject to create.
UnitButton_Script.selectedDefender,
// This is WHERE to create them.
// CalculateWorldPointOfMouseClick returns the mouse position as
// world units.
// CalculateRoundedWorldPointOfMouseClick takes in the Vector2 from
// CalculateWorldPointOfMouseClick and rounds the X and Y values to
// the nearest integer (HOWEVER, I've kept the numbers as floating
// point values)
CalculateRoundedWorldPointOfMouseClick (
CalculateWorldPointOfMouseClick ()
),
// This is what rotation to create the GameObject with.
// Quaternion.identity essentially returns the same rotation the
// GameObject has in its prefab.
Quaternion.identity,
// This is the transform of the GameObject the created GameObject
// should be childed to.
defendersParent.transform
);
// I don't know if this option exists before Unity3D 5.X.
// If it doesn't, you'll need to store the GameObject in a variable and
// manually child the newly created GameObject to its intended parent.
// e.g. GameObject obj = Instantiate<GameObject>(def, pos, rot);
// obj.transform.parent = defendersParent.transform".
}
}
Vector2 CalculateWorldPointOfMouseClick() {
return Camera.main.ScreenToWorldPoint (Input.mousePosition);
}
Vector2 CalculateRoundedWorldPointOfMouseClick(Vector2 point) {
Vector2 newPoint = new Vector2(Mathf.Round(point.x), Mathf.Round(point.y));
return newPoint;
}
Vector2 CalculateRoundedWorldPointOfMouseClick(Vector3 point) {
Vector2 newPoint = new Vector2(Mathf.Round(point.x), Mathf.Round(point.y));
return newPoint;
}
}