Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

AddForce to instantiated GameObject

Discussion in 'Scripting' started by Foxi, Sep 13, 2020.

  1. Foxi

    Foxi

    Joined:
    Feb 13, 2014
    Posts:
    4
    Hi, i want my character to throw shurikens from his hand in the direction of mouse.
    Now it just spawns and fall down without any force.

    Code (CSharp):
    1. public GameObject starPrefab;
    2.     public Transform hand;
    3.     public Transform player;
    4.     public float speed;
    5.  
    6.  
    7.  
    8.  
    9.     void Update()
    10.     {
    11.  
    12.  
    13.  
    14.         Vector3 mousePos = new Vector3(Input.mousePosition.x, Input.mousePosition.y, 12);
    15.         mousePos = Camera.main.ScreenToWorldPoint(mousePos);
    16.  
    17.         if (Input.GetMouseButtonDown(1))
    18.         {
    19.             Instantiate(starPrefab, hand.transform.position, player.rotation);
    20.             starPrefab.GetComponent<Rigidbody>().AddForce((mousePos) * speed);
    21.         }
    22.  
    23.  
    24.  
    25.     }
    if I add ,,starPrefab =" before Instantiate, It's just spawning (clone), (clone)(clone),(clone)(clone)(clone) etc. and correctly adding force to the mouse. I wouldn't have a problem with that if spawning stopped working when last (clone) is destroyed (simple timed self destroy script on the star prefab). So how do I add the force to this instantiated object?
     
  2. Adrian

    Adrian

    Joined:
    Apr 5, 2008
    Posts:
    1,061
    Prefabs are the blueprint and
    Instantiate
    returns the instance created from the blueprint. Right now, you're trying to add force to the prefab, which will have no effect. If you overwrite the
    starPrefab
    variable, you will then create copies of the instance in the scene, and when that instance is deleted, you won't be able to create more.

    Instead, assign the instance to a new variable:
    Code (CSharp):
    1. var instance = Instantiate(starPrefab, hand.transform.position, player.rotation);
    2. instance.GetComponent<Rigidbody>().AddForce((mousePos) * speed);
     
    Styxu, thatonekidplayz, FOAG and 2 others like this.
  3. Foxi

    Foxi

    Joined:
    Feb 13, 2014
    Posts:
    4
    That works, kind of, finally spawning and throwing not just falling. No matter if character facing left or right, on right screen the force is as it should be, on left side the force is weaker.
    Failed to upload video, so at least this: https://media0.giphy.com/media/H682ThA0xKhgBtZzcn/giphy.gif Not the best quality, but still can see what the problem is.

    Edit: Problem solved as soon as I posted this reply. In the code
    Code (CSharp):
    1. var instance = Instantiate(starPrefab, hand.transform.position, player.rotation);
    I just added transform to the player.rotation
    Code (CSharp):
    1. var instance = Instantiate(starPrefab, hand.transform.position, player.transform.rotation);
     
    Last edited: Sep 13, 2020
  4. thatonekidplayz

    thatonekidplayz

    Joined:
    Jan 11, 2023
    Posts:
    1
    @Adrian i did this, but i got a compiler error saying that i don't have rigidbody on my newly instantiated object, aka my bullet, and i don't know how to get components onto the prefab. help?
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    Please don't necro-post to ancient unrelated threads. Set yourself up for success: start a fresh post.

    When you post, here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    matyn04 likes this.