Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

Question Instantiated rigidbody2d can't add force.

Discussion in 'Physics' started by ocatrina, Apr 1, 2022.

  1. ocatrina

    ocatrina

    Joined:
    Mar 17, 2014
    Posts:
    1
    Any help is appreciated because I haven't moved from this chair in hours...working on the same problem!
    I am a newbie and am learning mostly from the "school of hard knocks" although I have completed several Unity tutorials. My problems are: 1) How do I get the instantiated object to be recognized in the FixedUpdate() if I have spawned it in another method? I don't believe it is being recognized which is why I may have the second problem. (I did have it working, but, I've changed it so much, I don't know why...ugh. 2) Based on the first problem, I can't get AddForce to work with my instantiated rigidbody2D...or does AddForce need to be added to the var Rigidbody2D rb?

    The prefab is dragged into the gameobjects necessary. However, I don't have a script on my prefab, which simply is supposed to drop on the press of the space bar. I put it in an empty gameObject so that I didn't have that first dangler that would drop without interaction.

    Soooo, it's 8PM and haven't eaten yet...Hopefully some of you brainiacs can help me with this probably-simple-solution. Thank you in advance!

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class HoldObject : MonoBehaviour
    4. {
    5.     public Rigidbody2D rb;
    6.     //public Rigidbody2D instantiatedObj;
    7.     public GameObject perimeter;
    8.     //Vector3 pos;
    9.     int count = 0;
    10.     bool down;
    11.     bool right;
    12.     bool left;
    13.     public static bool isCreated;
    14.    
    15.  
    16.    
    17.     private void Awake()
    18.     {
    19.         if (rb != null)
    20.         {
    21.             rb.GetComponent<Rigidbody2D>().position = Vector2.zero;
    22.         }
    23.  
    24.         Score score = gameObject.AddComponent<Score>();
    25.     }
    26.  
    27.     void Start()
    28.     {
    29.         //count = 0;//working on this
    30.     }
    31.  
    32.  
    33.     void FixedUpdate()
    34.     {
    35.         down = Input.GetKey(KeyCode.Space);
    36.         right = Input.GetKey(KeyCode.RightArrow);
    37.         left = Input.GetKey(KeyCode.LeftArrow);
    38.         if (!isCreated)
    39.         {
    40.             if (down)
    41.             {
    42.                 SpawnIt();
    43.                 //count++; working on it
    44.             }
    45.  
    46.             else if (right)
    47.             {
    48.  
    49.                 if (rb != null)
    50.                 {
    51.                     rb.AddRelativeForce(Vector2.right);
    52.                     Debug.Log("pressing right");
    53.                 }
    54.             }
    55.             else if (left)
    56.             {
    57.                 if (rb != null)
    58.                 {
    59.                     rb.AddForce(Vector2.left);
    60.  
    61.                     Debug.Log("pressing left");
    62.                 }
    63.             }
    64.         }
    65.     }
    66.     void SpawnIt()
    67.     {
    68.  
    69.         MeshCollider c = perimeter.GetComponent<MeshCollider>();
    70.  
    71.         //float screenX = Random.Range(c.bounds.min.x, c.bounds.max.x);//doesn't work so used absolutes
    72.         float screenX = Random.Range(-6.0f, 6.0f);
    73.         //float screenY = Random.Range(c.bounds.min.y, c.bounds.max.y);
    74.         float screenY = Random.Range(5.0f, 10.0f);
    75.         Vector2 pos = new Vector2(screenX, screenY);
    76.  
    77.         if (screenY <= Random.Range(c.bounds.min.y, c.bounds.max.y) && screenX <= Random.Range(c.bounds.min.x, c.bounds.max.x))
    78.         {
    79.             this.transform.position = new Vector2(transform.position.x, transform.position.y - screenY);
    80.         }
    81.  
    82.         else if (screenX <= Random.Range(c.bounds.min.x, c.bounds.max.x))
    83.         {
    84.             this.transform.position = new Vector2(transform.position.x, transform.position.y + screenY);
    85.         }
    86.         Rigidbody2D instantiatedObj = Instantiate(rb, pos, rb.transform.rotation);
    87.         isCreated = true;
    88.     }
    89.  
    90. }
     
  2. RafaelGomes00

    RafaelGomes00

    Joined:
    Aug 13, 2020
    Posts:
    73
    Answering your questions:
    1) You can access the rigidbody instance of an instantiated object using global variables, basically creating variables outside methods (that would be local variables), and access it. so what you can do is:

    Rigidbody2D instantiatedObj; (Oustide any method)
    instantiatedObj = Instantiate(rb, pos, rb.transform.rotation); (at line 86)

    2) To add a force to a object that you instantiated you will have to use the instantiatedObj variable that you created on global scope (on first answer) and use this variable to add force, for example: instantiatedObj.AddForce( [Method stuff] );

    If you try to use rb.AddForce, it will only make the object that you passed through the inspector to move. And you don't want that, because the instantiated object will be created at runtime.