Search Unity

Player GameObject not updating position in game after position reassignment in script

Discussion in 'Editor & General Support' started by unity_dIY5WbqQ95X8-w, Jan 15, 2021.

  1. unity_dIY5WbqQ95X8-w

    unity_dIY5WbqQ95X8-w

    Joined:
    Jul 22, 2019
    Posts:
    6
    ```

    else
    {
    Debug.Log("Deployed: True");
    if (clonedHusk != null)
    {
    Debug.Log($"Husk exists. sP P: {spawnPosition.position} sP R: {spawnPosition.rotation}");
    Debug.Log($"Husk P: {clonedHusk.transform.position} - Husk R: {clonedHusk.transform.rotation}");
    spawnPosition.position = clonedHusk.transform.position;
    spawnPosition.rotation = clonedHusk.transform.rotation;
    Debug.Log($"Name: {spawnPosition.gameObject.name}");
    Debug.Log($"Husk being destroyed. sP P: {spawnPosition.position} sP R: {spawnPosition.rotation}");

    Destroy(clonedHusk);
    Debug.Log($"Husk destroyed - sP P: {spawnPosition.position} sP R: {spawnPosition.rotation}");
    isDeployed = false;
    }
    }
    ```
    I have a GameObject Transform spawnposition. This spawnposition is the player, where the husk will be spawned. Whenever I call this function, the rotation is updated, but the position is not. The Debug.Log says that the position has been changed to where the husk was, but it doesn't update in the game scene. How do I get it to change positions?
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,196
    I would look at whether any other code is also changing just the position of the object. Maybe something in another script, like maybe something in Start on some script on the spawned object.

    Your code also confuses me a little, but maybe it's fine. You're assigning the clonedHusk's position and rotation to the spawnPosition object. I would normally expect it to be the other way around. But I guess when the husk dies, you're just keeping track of its position and rotation in spawnPosition, so you can do something with that later?
     
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    CharacterController can cause this issue. Does your character have a CharacterController on it?