- Home /
Instantiated object's transform changes according to parent object's rotation
I have an Object(parent) having 4 spawn points around it which spawns stars. In the game, Parent object rotates but the Spawned stars don't move according to Parent objects rotation. I have tried all the suggestions people have given in the forum.
public GameObject starPrefab;
private GameObject newStarPrefab;
public Transform[] starSpawnPoints;
void Start () {
if (!GameObject.FindGameObjectWithTag("Box"))
{
RandomPoint();
}
}
void RandomPoint()
{
int randomIndex = Random.Range(0, starSpawnPoints.Length);
for (int i = 0; i < starSpawnPoints.Length; i++)
{
if (randomIndex == i)
{
newStarPrefab = Instantiate(starPrefab, starSpawnPoints[i].position, Quaternion.identity);
newStarPrefab.transform.parent = gameObject.transform;
}
}
}
I have added this script to the Parent object which rotates after spawning.
You're saying the parent has this script, the spawn points are it's children and after creating stars and attaching them to those they don't normally rotate with it?
Answer by tormentoarmagedoom · Sep 14, 2017 at 02:04 PM
Hello @Ki4Chan !
As i can see, you instantiate the objects with position starSpawnPoints[i]. As i understand, this transfomrs are the spawns transforms that should move when the parent rotate.
In the scene, do you see the spawns moving? I can't tell you why is happen this, but try this things to check:
Try to chech if the spawn poitns move when parent rotates. before Instantiating the starPrefab, do a
foreach (Transform TransformSpawn in starSpawnPoints)
{
Debug.Log (TransformSpawn.position + " - " + TransformSpawn.name)
}
To check all SpawnPoints position. Or even try it in the Update if the is rotating all time
You can also try to Instantiate directly to be the child of a transform, using this:
Instantiate (starPrefab, transform);
If this helps, upvote and check the answer!
If need more help, give more info and use the @tormentoarmagedoom
Bye :D
Bro, the same code is working. All I did is closed Unity and reopened it. Thank you for your precious time.
Your answer
Follow this Question
Related Questions
Player not facing the mouse correctly 1 Answer
GameObject facing fowards 1 Answer