• 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
Question by Inceptix · Nov 17, 2021 at 08:50 PM · instantiateprefabrigidbody2dgamobject

GameObject is not reading the rigidbody2D component of instantiated prefab,GameObject is not reading the Rigidbody2D component from instantiated prefab

I am making a Pong game to practice Unity. I have a GameManagerPong.cs that spawns in the ball.

 public class GameManagerPong : MonoBehaviour
 {
     public GameObject ballPrefab;
     // Start is called before the first frame update
     void Start()
     {
         Instantiate(ballPrefab, new Vector2(0, 0), Quaternion.identity);
     }
     
     public void SpawnBall()
     {
         StartCoroutine(ballTimer());
     }
 
     IEnumerator ballTimer()
     {
         yield return new WaitForSeconds(1.5f);
         Instantiate(ballPrefab, new Vector2(0, 0), Quaternion.identity);
     }
 }

I also have a ball prefab w$$anonymous$$ch has a Rigidbody2D component. I have two paddles in the scene. Paddle1(player paddle) and paddle2(AI paddle) Paddle2 has public gameObject reference to the prefab Ball.

alt text

Also t$$anonymous$$s is the code in the ComputerPaddle script

 public class ComputerPaddle : Paddle
 {
     public GameObject ball;
     private Rigidbody2D rbBall;
 
     [Tooltip("Larger number makes the paddle react more slowly")]
     public float computerMoveThreshold = 3.0f; //increase number to make paddle move with delay
 
     private void Start()
     {
         rbBall = ball.GetComponent<Rigidbody2D>();
     }
     private void FixedUpdate()
     {
         Debug.Log(rbBall.velocity); //t$$anonymous$$s just returns (0.0, 0.0)
         if (t$$anonymous$$s.rbBall.velocity.x > 0.0f && t$$anonymous$$s.rbBall.position.x > computerMoveThreshold)
         {
             if (t$$anonymous$$s.rbBall.position.y > t$$anonymous$$s.transform.position.y)
             {
                 rb.AddForce(Vector2.up * t$$anonymous$$s.speed);
             }
             else if (t$$anonymous$$s.rbBall.position.y < t$$anonymous$$s.transform.position.y)
             {
                 rb.AddForce(Vector2.down * t$$anonymous$$s.speed);
             }
         }
         else
         {
             if(t$$anonymous$$s.transform.position.y > 2.0f)
             {
                 rb.AddForce(Vector2.down * t$$anonymous$$s.speed);
             }
             else if(t$$anonymous$$s.transform.position.y < -2.0f)
             {
                 rb.AddForce(Vector2.up * t$$anonymous$$s.speed);
             }
         }
     }
 }

At first I just tried making a public Rigidbody2D variable and dragged the Ball prefab on there and it still didn't work. T$$anonymous$$s version also doesn't work. T$$anonymous$$s may be a simple fix but I'm a bit stumped. Any help is appreciated. Thank you!

unity-questtion.png (10.3 kB)
Comment

People who like this

0 Show 2
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 Inceptix · Nov 17, 2021 at 01:49 AM 0
Share

I wanted to add that before making the ball a prefab and spawning it in everything worked as intended. The AI paddle would move accordingly. Now it doesn't react at all to the balls position.

avatar image Inceptix · Nov 17, 2021 at 02:14 AM 0
Share

Furthermore, if I put the prefab in the scene and drag in the ball from the hierarch into the ball slot of ComputerPaddle script the AI paddle will work until that ball gets destroyed, which makes sense. But if I leave the prefab in the scene and drag the prefab from the assets into the script then it stops working.

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by bdubbert · Nov 17, 2021 at 10:47 PM

You can t$$anonymous$$nk of a prefab like a GameObject that doesn't exist in a scene. It exists somewhere, but not in any place that it can interact with anyt$$anonymous$$ng. When you Instantiate a prefab, you are making a copy of it that does exist in the scene. In fact, you can make as many copies of t$$anonymous$$s object as you want in the scene.


So if you have a reference to a prefab then you instantiate that prefab, your reference is still the original prefab object, NOT the one that was just copied into the scene, w$$anonymous$$ch is what you want. So how do you get a reference to the instance of the prefab?


Well, there are a million different ways. The easiest way is for whatever object is spawning the ball (your game manager in t$$anonymous$$s case) to just keep track of the ball, and give out references whenever another script asks.

 public GameObject theBallInstance;
 theBallInstance = Instantiate(ballPrefab...);
 ...
 //Get the ball instance from another script
 GameObject theBall = gameManager.theBallInstance;
 

Just make sure that the ball is actually instantiated before you ask the game manager for the ball instance ($$anonymous$$nt - put your Instantiate call in Awake, because your other script is accessing the ball on Start).


You can also use calls like GameObject.Find, GameObject.FindObjectOfType, to dynamically find objects or components in a scene, but keep in mind that these are expensive and shouldn't be called every frame.

Comment
Inceptix

People who like this

1 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

187 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 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 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

BoxCollider2D failling verification when Instantiate (Fixed, Cause: big derp) 1 Answer

When flipping player, instantiated objects spawn on different spot. 2D shooting 0 Answers

Prefab instead of class oClass = new class() ? 1 Answer

Get unity to recognize prefab in C# 2 Answers

Instantiate prefab with different values each time 4 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