0
votes

I have spheres being instantiated as children of a GameObject. I want these spheres to follow the Position of the GameObject.

The trouble is, these spheres stubbornly stay in the place they were spawned in, and refuse to change their position upon any Transform position changes in Inspector during runtime.

spheres = new GameObject[mesh.vertexCount]; //create gameobject array, size of mesh vertex count

for (int i = 0; i < mesh.vertexCount; i++)
{
    matrices1pos[i] = matrices1[i].position;
    spheres[i] = Instantiate(spherePrefab, matrices1[i].position, Quaternion.identity);
    spheres[i].transform.SetParent(GO2.transform);
}

I even tried adding code:

spheres[i].transform.localPosition = Vector3.zero;

but that doesn't make the spheres respond to parent's position changes either.

By the way, the location the Spheres spawn in is the location of the GameObject (called MeshChanger) the script runs on. Changing the position of MeshChanger transform during runtime has no effect either.

edit: Solved! see answer below

1
How do you set GO2? Are you sure that it's not a reference to a prefab?Ruzihm
Sorry, I'm not entirely sure what you mean as I'm newbie. In MeshChanger.cs I have public GO2 GO2; before any functions, and for gameobject GO2 I simply have script GO2.cs attached which says public Transform Transform { get; set; }rashapou
I mean, do you set the value of GO2 using the inspector? If so, make sure you are referring to a GO2 that is on a game object that is in the scene hierarchy.Ruzihm

1 Answers

0
votes

I figured it out. I needed to use localPosition. =]

spheres[i].transform.SetParent(GO2.transform);
spheres[i].transform.localPosition = Vector3.Lerp(matrices1pos[i], matrices2[i].position, t);