Instantiated object(child) to move according to Instantiated ParentObject'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.

The spawner stars are Particle Effects?
If so, have you checked the “Simulation Space” as “Local”, instead “World”?

And why you do this instead this?

void RandomPoint()
        {
            int randomIndex = Random.Range(0, starSpawnPoints.Length);

                    newStarPrefab =  Instantiate(starPrefab, starSpawnPoints[randomIndex ].position, Quaternion.identity);
                    newStarPrefab.transform.parent = gameObject.transform;

        }

no, Stars are not particle effects.

mhhh… maybe they have a RigidBody? In that case they follow the phisics instead parents

that code works here (and they rotate with parent)… so what is your starPrefab like?

it’s a normal 2D sprite. It does not have any other component yet.

No they do not have any additional component yet.

I have used “randomIndex != 1” In my previous project. I just made little changes in new one for my future convenience. :slight_smile:

 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;
                }
            }

You move the 2D Sprite? maybe some animation?

I closed Unity opened it again, now it’s working. Same script, nothing changed.

1 Like

LOL, the programmer method works always,
see you soon