• 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 /
  • Help Room /
avatar image
0
Question by brudi · May 04, 2016 at 02:06 PM · unity 5updaterigidbody2daddforceforce

Instantiated Prefab no Rigidbody2D on Update function

Hey there,

I have the following situation:

I got a GameplayManager script like this:

 public class GameplayManager : MonoBehaviour
 {
     public Transform spawnPoint1;
     public Rigidbody2D player;
 
     void Start()
     {
         Instantiate (player, spawnPoint1.position, spawnPoint1.rotation);
     }
 
         ....
 }

There is a prefab assigned to the "player" variable there is an empty gameobject assigned to the spawnPoint1 variable.

This script-component(GameplayManager) is attached to the camera component.

On my "player" prefab i assigned a new script component "PlayerManager":

 public class PlayerManager : MonoBehaviour
 {
     Rigidbody2D rb2d;
 
     void Start()
     {
         rb2d = GetComponent<Rigidbody2D> ();
     }
 
     void Update()
     {
         
     }
 
     public void Jump()
     {
         rb2d.AddForce (new Vector2 (rb2d.velocity.x, 500f));
     }
 
     ...
 }

Next i added a Button to my canvas and assigned a Event Trigger: Value: "Player" Prefab Select: PlayerManager.Jump()

When i press the button in the game, no force is applied to the rigidbody of the player prefab.

I simply dont know why..any help or advices?

Thansks and Greetings

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

1 Reply

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

Answer by incorrect · May 04, 2016 at 02:18 PM

You should understand the difference between a prefab and it's instance. A prefab is just a prototype that exists in a memory but not in a scene. An instance is a copy built from this prototype and placed into the scene.

So if you are calling a method on a prefab you are not calling a method on an actual instance you've created using Instantiate().

To call a method on the instance of the prefab you need to store a reference to this copy an use it like reference.method();

Try saving a reference when you instantiate the prefab in GameplayManager and assign it's method to the button:

 [SerializeField]
 private UnityEngine.UI.Button button;
 
 void Start()
 {
     //saving reference to a copy created
     GameObject instance = Instantiate (player, spawnPoint1.position, spawnPoint1.rotation) as GameObject;
         
     //getting PlayerManager on this copy
     PlayerManager pManager = instance.GetComponent<PlayerManager>();
         
     //if there is a PlayerManager on this copy
     if(pManager != null)
         //assign instance's method to the button onClick delegate
         button.onClick.AddListener(pManager.Jump);
 }

Comment
Add comment · Show 5 · 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 brudi · May 04, 2016 at 06:24 PM 0
Share

thanks this was my mistake, i will still have to deal with some game logics in the future:)

avatar image brudi · May 04, 2016 at 09:34 PM 0
Share

But i still have a question... when i call Jump method, the rigidbody still needs to be created in "Player$$anonymous$$anager" right, and in this case its unassigned... any advices?

Between: Shouldnt you change

 button.onClick.AddListener(instance.Jump);

to:

 button.onClick.AddListener(p$$anonymous$$anager.Jump);

?

avatar image incorrect brudi · May 04, 2016 at 09:50 PM 1
Share

Yes, my bad. button.onClick.AddListener(p$$anonymous$$anager.Jump); is the right one. I'll edit my answer, thnx.

You don't need to create RigidBody2D if your prefab GameObject has this component. As I understand, you also have Player$$anonymous$$anager component on this prefab.

Your Player$$anonymous$$anager has Start() method that is executed once object is created. This code actually find a reference to the Rigidbody2D and saves it into rb2d variable.

   rb2d = GetComponent<Rigidbody2D> ();

So when Jump() is called, you already have a reference to rigidbody.

avatar image brudi incorrect · May 05, 2016 at 12:00 PM 0
Share

do i need this line:

 button.onClick.AddListener(p$$anonymous$$anager.Jump);

or can i just apply an "event trigger" to the button in the canvas?

Show more comments

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Predicting max jump height of a force impulse 1 Answer

Addforce trajectory 0 Answers

Impulse Force not smooth 0 Answers

2D Top-Down Character: How to make my character move using physics? 1 Answer

Bullets falling through the terrain 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges