Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by lukeatmycode · Feb 09, 2018 at 09:25 PM · spawning

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lukeatmycode · Feb 09, 2018 at 09:38 PM 0
Share

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.

avatar image nathanvj lukeatmycode · Feb 09, 2018 at 09:39 PM 0
Share

Glad you figured it out yourself. I read your comment right after I posted my answer. :-)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

77 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges