- Home /
This question was
closed Sep 20, 2018 at 11:18 PM by
polan31 for the following reason:
The question is answered, right answer was accepted
Question by
polan31 · Sep 20, 2018 at 09:53 PM ·
2d gamechild objectparent-child
How to create a child object of an object with a specific tag?
I would like the spheres that appear after pressing PLAY to be children objects of the object with the PLAYER tag.
How to do it?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PathPlacer : MonoBehaviour {
public float spacing = .1f;
public float resolution = 1;
void Start () {
Vector2[] points = FindObjectOfType<PathCreator>().path.CalculateEvenlySpacedPoints(spacing, resolution);
foreach (Vector2 p in points)
{
GameObject g = GameObject.CreatePrimitive(PrimitiveType.Sphere);
g.transform.position = p;
g.transform.localScale = Vector3.one * spacing * .5f;
}
}
}
Comment
Best Answer
Answer by LCStark · Sep 20, 2018 at 10:01 PM
You can assign the parent object of the newly created GameObject
:
g.transform.parent = someObject.transform;
You can get object by tag using GameObject.FindWithTag.