Transform Position is not moving the target
public Player player;
This is how my player script is referenced in the HealthManager script. I am trying to call this:
public void Respawn()
{
player.transform.position = respawnPoint;
// currentHealth = maxHealth;
}
Respawn point, is initialized in the Start() funtion, and it is not zero. It has the coordinates of the place where player spawned. But it doesn't move the player at all. See also the image attached.
Could anyone help, why does the
player.transform.position = respawnPoint;
move the player? Also, player is NOT set to static
Answer by TimeToSparkz · Aug 11, 2019 at 09:05 AM
You have made mistake in this line: public Player player
You cannot use transform.position of script, use GameObject instead of Player: public GameObject player
$$anonymous$$isleading and wrong. Player is (most likely) a $$anonymous$$onoBehaviour and therefore a Component. Every component does have a transform property to access the Transform component of the same gameobject.
Answer by Bunny83 · Aug 11, 2019 at 09:47 AM
Well, what have you done so far to track down your issue? Every one would start by adding a Debug.Log("respawning player", gameObject);
inside your Respawn method to see if it get called properly. Note that giving the context object allows you to see which object has actually produced this message. When you click on the message in the console Unity will ping that object in the hierarchy / project. So if the method is called, make sure it comes from the correct object.
If it runs correctly on the GameManager object, see if your "Player" reference actually references the player in your scene and not some prefab in the project.
If everything looks right make sure you don't have any other code that manipulates your player position at the same time.
So you can also do this inside Respawn:
Debug.Log("about to respawn the player (currently located at " + player.transform.position + ") at the position " + respawnPoint);
player.transform.position = respawnPoint;
Debug.Log("player has been respawned at " + player.transform.position);
Your answer
Follow this Question
Related Questions
Cannot properly access player position 1 Answer
How to get the position of a gameObject based on its index in an array? 0 Answers
How To Make An Object Appear In Front Of Player Without Cloning? 1 Answer
Transform GameObject position with random range 0 Answers
How to get continuous reference to a GameObject's position? 2 Answers