- Home /
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.
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!
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.
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.
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.
Your answer
Follow this Question
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