- Home /
Why does instantiating a prefab break the original's behaviour
I am trying to create a simple version of Quidditch from Harry Potter. I've got two opposing teams, that try to touch the gold ball that flies randomly. When I have two game objects in the scene, they chase perfectly. However, when I then try to spawn more teammates, the previous player stops tracking the gold ball. I have been scouring the internet and I haven't been able to figure this out. Please help me.
Here is the player script:
public class Gryffindor : MonoBehaviour {
public static float maxVelocity = 16;
public static float maxAcceleration = 20;
public static double tacklingProb = 0.3;
private static Rigidbody rb;
private static GameObject snitch;
private static GameObject[] enemy;
private static GameObject[] team;
// Use this for initialization
void Start () {
rb = GetComponent<Rigidbody> ();
rb.useGravity = false;
snitch = GameObject.FindGameObjectWithTag ("Snitch");
enemy = GameObject.FindGameObjectsWithTag("S");
team = GameObject.FindGameObjectsWithTag("G");
}
// Update is called once per frame
void Update () {
//transform.rotation = Quaternion.LookRotation (rb.velocity, Vector3.up);
rb.AddForce ((snitch.transform.position - transform.position).normalized*maxAcceleration*Random.value, ForceMode.Acceleration);
}
void tackled (){
rb.useGravity = true;
}
void OnCollisionEnter(Collision enteredCollider) {
if (enteredCollider.gameObject.CompareTag ("KillZone") && rb.useGravity)
Destroy (gameObject);
}
}
And here is the spawner script:
public class GSpawn : MonoBehaviour {
public int playerCount;
public GameObject gryfPrefabs;
GameObject gryClone;
// Use this for initialization
void Start () {
//playerCount = 0;
}
void FixedUpdate () {
if(Input.GetKeyDown("d")) {
spawnPlayer();
}
}
void spawnPlayer () {
gryClone = Instantiate(gryfPrefabs,transform.position, Quaternion.identity) as GameObject;
gryClone.transform.position = transform.position;
}
}
I have the spawner linked in the inspection window as well. Thank you.
Answer by nathanvj · Feb 09, 2018 at 09:38 PM
The RigidBody variable rb shouldn't be marked as static. The Rigidbody will be different for every Gryffindor, when you're creating a new Gryffindor, the rb variable is assigned the Rigidbody of the new Gryffindor. Therefore the logics in your Update() function will only be executed on the new Gryffindor's Rigidbody.
You should watch some tutorials on statics, it seems like you're using them excessively.
Answer by spidermancy612 · Feb 09, 2018 at 09:34 PM
So a couple questions to help find the answer.
1) Can we get a gif or video of the bug?
2) Can we see the player controller or is there a player control script?
3) When you spawn another player, use Debug.Log to print out what the current player target is to see if it still has the right reference. Let us know what you get.
Thank you for getting back to me! I really appreciate the attempt to help me, but I actually found the solution right after I posted this question.
I was using static variables for the rigid body, so they ended up all sharing the same body.
I will make sure to use Debug.log in the future as I continue to use Unity.
Glad you figured it out yourself. I read your comment right after I posted my answer. :-)
Your answer
Follow this Question
Related Questions
Spawning Objects 1 Answer
Random loot - rarity 3 Answers
error CS1502 and CS1503 1 Answer
Spawning A Grid Of Cubes 2 Answers
help with spawning objects 2 Answers