- Home /
Game Object Teleport's to to players starting position.
I'm trying to change a "MashroomFullgrown's" position to that of the Player's. However the MashroomFullgrown get's sent to the position that the player started the game at instead of the Player's current position.
void Update () {
if (harvested == true)
MashroomFullgrown.transform.position = Player.transform.position;
}
},I'm trying to change a "MashroomFullgrown's" position to that of the Player's. However the MashroomFullgrown get's sent to the position that the player started the game at instead of the Player's current position.
Answer by Larry-Dietz · Dec 08, 2017 at 01:00 AM
Assuming that there is only one GameObject with a script called Player on it, then do this in the code for your MashroomFullGrown
In the declarations at the top...
Player myPlayer;
In Start...
myPlayer = FindObjectOfType<Player>();
Then in the relocation ...
MashroomFullgrown.transform.position = myPlayer.transform.position;
This will create a local reference to the Player.
Hope this helps, -Larry
This worked perfectly! I've been trying at this for hours, thanks a ton for your help.
You are very welcome. I have been stuck many times myself :) Glad I could help.
Answer by remy_rm · Dec 07, 2017 at 10:29 PM
Player.transform.position
appears to be calling to the Player class, instead of to an instantiated object of the player, make sure you're actually refering to the actual player object.
if your player is a child of another object then try using player.transform.localPosition/convert it to world space.
(on a side note; if you're certain that "Player" is a variable of your object then consider renaming it to "player" to keep in line with naming conventions where variables are in camelCase, and Class names are in PascalCase)
Answer by miningcheeseman · Dec 08, 2017 at 12:32 AM
I found that the issue (probably) didn't have to do withe the script, after trying your suggestions and doing some testing, I found that the player game object that the script is targeting is in my prefabs folder. But for the life of me I can't get it to instead get the Player game object from my scene/game view.
Hopefully that made sense, and thanks for responding. :)