Search Unity

Instantiated prefab object cannot move itself with addForce

Discussion in 'Scripting' started by Unconnected42, Jul 26, 2016.

  1. Unconnected42

    Unconnected42

    Joined:
    Jul 26, 2016
    Posts:
    7
    Hi,

    In my project, I have a player that nicely moves itself with addForce with script code like this:

    Code (CSharp):
    1.  
    2. void FixedUpdate() {
    3.    /// ...
    4.    myrigidbody.addForce(  someForce );
    5.    /// ...
    6. }
    Then, for some reason, I instantiate with a prefab a slightly different version of my player.
    (well, it is the same player actually for now, it is just destined to be a slightly different kind of player later because said player is some kind of spaceship and I want people who will play the game to be able to change their ship type if they want)

    The code for that goes like this :

    Code (CSharp):
    1.  
    2. GameObject newMe = (GameObject) Instantiate ( Resources.Load( somePathToPrefab ), transform.position, transform.rotation);
    3. Object.Destroy(this.gameObject);
    And it works ! The new player appears, is located at the right place, facing the right direction, and even rotating if I ask him to.

    The only thing is, it won't move, meaning the addForce from the first bit of code is not working any more when working inside the instantiated prefab object instead of the original from inside the editor.

    Does someone have any idea of the reason ?
     
  2. PhenixEmporium

    PhenixEmporium

    Joined:
    Jul 24, 2016
    Posts:
    22
    That is very strange, perhaps if I saw more of the script that contains the "FixedUpdate" function, I could be of assistance.
     
  3. Errorsatz

    Errorsatz

    Joined:
    Aug 8, 2012
    Posts:
    555
    Does the new object you instantiate have the script with FixedUpdate on it?
     
  4. Unconnected42

    Unconnected42

    Joined:
    Jul 26, 2016
    Posts:
    7
    Here is a simplified version of the script, which also triggers the same bug :

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class SimplePlayerController : MonoBehaviour {
    6.    
    7.     public Text infoText;
    8.     private float thrust;
    9.     private Rigidbody rb;
    10.    
    11.     void Start() {
    12.         rb = GetComponent<Rigidbody>();
    13.         thrust = 0f;
    14.     }
    15.    
    16.     // Does things before updating frame.
    17.     void Update() {  
    18.         if (Input.GetKeyDown(KeyCode.Space)) {
    19.             GameObject newMe = (GameObject)Instantiate(Resources.Load("Prefabs/ClassA2"),transform.position,transform.rotation);
    20.             newMe.GetComponent<SimplePlayerController>().infoText = GetComponent<SimplePlayerController>().infoText;
    21.             Object.Destroy(this.gameObject);
    22.         }
    23.         if (Input.GetKeyDown(KeyCode.UpArrow)) {
    24.             thrust = thrust + 0.1f;
    25.         }
    26.         if (Input.GetKeyDown(KeyCode.DownArrow)) {
    27.             thrust = thrust - 0.1f;
    28.         }
    29.     }
    30.    
    31.     // Does things before applying physics, at fixed time.
    32.     void FixedUpdate() {
    33.         Vector3 movement = transform.forward * thrust;
    34.         Vector3 vel = rb.velocity;
    35.        
    36.         infoText.text = "Velm:" + vel.magnitude.ToString() + " ; Thrust:" + thrust.ToString();
    37.         rb.AddForce( movement );
    38.     }
    39.    
    40.    
    41. }
    42.  
    To answer Errorsatz, the instantiated prefab is made of a prefab that was created from the first player and is strictly identical. So, yes, they have the same script...
     
  5. Unconnected42

    Unconnected42

    Joined:
    Jul 26, 2016
    Posts:
    7
    After some more research, I found that any kind of prefab that I instantiate will not move at all (whether it's the player or not), and it wont move even if I try to move it inside the player script (without deleting the player in that case).

    If the lines 19-21 are basically replaced with :

    Code (CSharp):
    1.             GameObject newObj = (GameObject)Instantiate(Resources.Load("Prefabs/Test"),transform.position + transform.forward,transform.rotation);
    2.             newObj.GetComponent<Rigidbody>().AddForce(transform.forward);            
    The object will not move either.


    Is there something wrong in the way I am handling the calls to the new object's Rigidbody ?
    Is there something else to do or setup with the newly created object for it to work properly ?

    I'm really stuck there...
     
  6. PhenixEmporium

    PhenixEmporium

    Joined:
    Jul 24, 2016
    Posts:
    22
    I just took that same code and pasted it into the "Start" function of a new C# script in an empty unity project. It worked fine for me, though trying to move a Rigidbody through the use of AddForce at as low of a number as (transform.forward) is will make it seem as if it's not moving at all. Multiplied by 10 it was noticeable but still inching along. Perhaps that is what you are seeing with this test code. Though I'm confused as to why your player script wasn't working on the prefab once it spawned, I'm gonna do more testing and see what I come up with.

    EDIT: Alright yea even using your original script, it's working fine on my end, after using the script to spawn a new object pause the game and inspect the newly spawned copy and see if it indeed has the right scripts/etc. attached to it.
     
    Last edited: Jul 27, 2016
  7. Unconnected42

    Unconnected42

    Joined:
    Jul 26, 2016
    Posts:
    7
    Thanks for taking some time for this, this is really appreciated !

    When you say you 'inspect' the newly spawned copy, what is it that you do exactly ? I am very new to Unity so this might be an obvious thing.
    And the original script (the one with the ClassA2 Prefab) was clearly attached to the object spawned, because it does display things in a text element on screen, and at least the variable thrust had its value correctly modified by the keyboard input and its value correctly displayed.
     
  8. PhenixEmporium

    PhenixEmporium

    Joined:
    Jul 24, 2016
    Posts:
    22
    I merely meant to pause the game after spawning a clone, clicking on it in the editor and making sure it has all the scripts and such. But if it's text element is being modified correctly, then that's probably not it. Hmmm yea I'm having trouble thinking of what could possibly be causing this to occur, based on everything you've shown it should all be working correctly. Though on a side note that's unrelated to the problem, why do you have the controls set up so you have to keep tapping the arrow keys in order to increase or decrease thrust? Would've just holding them down work better?
     
  9. Unconnected42

    Unconnected42

    Joined:
    Jul 26, 2016
    Posts:
    7
    Okay, I had forgotten the possibility of pausing the game and looking objects in the editor...

    I did that, and... well, I found nothing suspicious in the Test (Clone) object.

    Concerning the thrust, it is just a legacy of the main code, where I want the player to be able to precisely decide the vessel's engine output, that's all.


    I kind of thought that everything looked normal. I found other topics with people with similar problems, but the way I'm doing things was basically based on the solution given to their problem, so...

    It would be really strange (and unlucky) if that was some kind of bug of Unity itself specific to the build I downloaded... I have version 5.3.5f1 for Windows 32 bits.
     
  10. PhenixEmporium

    PhenixEmporium

    Joined:
    Jul 24, 2016
    Posts:
    22
    Ah ok, makes sense, and yea you may want to try submitting a bug report and see if you can get any help that way, wish I could be more help.
     
  11. Unconnected42

    Unconnected42

    Joined:
    Jul 26, 2016
    Posts:
    7
    Guess what ?

    In a new project created from scratch, the bug does not happen...

    Thus, there is something wrong in my project. No idea what it could be, though...