Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 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 which 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 this 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); //this just returns (0.0, 0.0)
         if (this.rbBall.velocity.x > 0.0f && this.rbBall.position.x > computerMoveThreshold)
         {
             if (this.rbBall.position.y > this.transform.position.y)
             {
                 rb.AddForce(Vector2.up * this.speed);
             }
             else if (this.rbBall.position.y < this.transform.position.y)
             {
                 rb.AddForce(Vector2.down * this.speed);
             }
         }
         else
         {
             if(this.transform.position.y > 2.0f)
             {
                 rb.AddForce(Vector2.down * this.speed);
             }
             else if(this.transform.position.y < -2.0f)
             {
                 rb.AddForce(Vector2.up * this.speed);
             }
         }
     }
 }

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

unity-questtion.png (10.3 kB)
Comment
Add comment · 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
1
Best Answer

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

You can think 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 anything. 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 this 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, which 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 this 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 (hint - 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
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

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

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

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

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

[C#] How can I destroy instantiated prefabs when many are created with the same name? 2 Answers

Updating a variable on a script in an instanced object 1 Answer

Why is instantiated animator prefabs are not working properly? 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