transform.position is not updating the object's position
I am trying to reset the player's position after entering collision with a certain trigger, which is supposed to reset the game.
The game is reset, and the next level is generated, however my player remains at the position he was at. I tried using Debug.Log() to see if the transform.position was actually doing anything at all, and I was surprised to see that the position vector is updated correctly, at least for a brief moment?, however I can't see that in-game as the player's position is not updated.
What am I doing wrong here?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollisionManager : MonoBehaviour
{
public LabyrinthGenerator lbG;
void OnTriggerEnter(Collider col) {
if (col.tag == "Finish") {
Debug.Log("Current Position: " + transform.position);
transform.position = new Vector3(0, 1.2F, 0);
Debug.Log("New Position: " + transform.position);
lbG.resetLevel();
}
}
}
Looking at the above code, I see nothing obvious wrong, assu$$anonymous$$g the script is in fact attached to the player GameObject. You might be more likely to get help if you share the contents of the LabyrinthGenerator.resetLevel()
function (which should start with a capital R by the way), or better yet, your entire project
What does lbG.resetLevel
? It most likely overrides the position of the object
Answer by Emag2706 · Sep 17, 2020 at 08:18 AM
Thank you all for the help. LabyrinthGenerator.resetLevel() simply clears all the walls of the maze and generates another one randomly. Doesn't reset the position of the object.
As for the problem itself, it had to do something with the CharacterController. It apparently doesn't like it when you try to change the position of the object it is attached to externally. The solution was to temporarily disable the component before changing the position, and then re-enabling again.