- Home /
why does my movement function affect all objects in scene?
i have two scripts meshbuilder and gameFunctionality. gamefunctionality contains the movement script as well as the on startup gui and the holder of all variables. the meshbuilder script sets up the public vertices uvs triangles textures and object related items which are created by a click of the on start gui button.
when i only spawn one cube in the scene the movement script works fairly well for a very basic movement. but when i spawn a second or third etc.. the camera goes the the new cube which is what i want but the movement script is still attached to the old cube so it moves as well.
the meshbuilder cube section:
public void CubeMe()
{
k++;
h++;
//skele = GetComponent<Rigidbody>();
box = new GameObject ("Cube"+ k);
box.AddComponent<Camera>();
box.AddComponent<movement>();
MeshFilter Filtration = box.AddComponent<MeshFilter>();
Mesh CubeMorph = Filtration.mesh;
box.AddComponent<MeshRenderer>().material = Resources.Load("Red",typeof(Material)) as Material;
box.AddComponent<MeshCollider>().sharedMaterial = Resources.Load("flesh",typeof(PhysicMaterial)) as PhysicMaterial;
box.AddComponent<Rigidbody>();
box.GetComponent<MeshCollider>().convex = true;
box.GetComponent<MeshCollider>().sharedMesh = CubeMorph;
if you need the whole script or the other script ill post em thank you for the help
Answer by brunocoimbra · Nov 14, 2016 at 07:34 PM
Because you are only adding the movement component to the new cube, you need to remove the component from the old one.
edit: k = the number thats added to obj name changed h to equal k
than like you suggested i added a destroy function in my game functionality class
if (GUI.Button(new Rect(0,0,100,20),"Cube $$anonymous$$e!"))
{
cubebuild.Cube$$anonymous$$e();
Destroy(GameObject.Find("Cube"+h).GetComponent<movement>());
}
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Want to move 2d fish sprites in pond ! 0 Answers
Code Not Working 1 Answer
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer