- Home /
Using static methods to transform a position
Hi, I'm learning Unity and I got a problem I have two classes "Player" class and "DeathZone" class
my "Player" code, pretty much moves the character and the "DeathZone" it's supposed to call the a method (function) from Player to Respawn the character into the startline again
something like this:
if (other.gameObject.tag == "Player" && playerTriggerBox.isTrigger)
{
// Call the Respawn function of the Player class.
Player.Respawn();
Nooww....Visual tells me you can't put just a void it needs to be static
I said, ok, ok I'll make it static
public static void Respawn()
{
//for now is empty
}
and I created an static variable
static Vector3 respawnPoint;
void Awake()
{
// Setting up references
respawnPoint = new Vector3(-26.1f, -1.67f, 0f);
}
but that's All i'm stuck on how to make a "transform.position" I can't just put it because those are non-static
Answer by EdwinChua · Oct 02, 2017 at 04:49 AM
@Dianiki64 In order to use the Respawn
method in the player class (without the static
modifier), you would need to do this:
class Player : MonoBehaviour
{
Player player;
void OnStart()
{
player = gameObject.GetComponent<Player>(); //gets the Player component attached to the same gameobject as this script
}
void OnUpdate()
{
if (other.gameObject.tag == "Player" && playerTriggerBox.isTrigger) //respawn code here
{
// Call the Respawn function of the Player class.
player.Respawn();
}
}
}
Please refer to a gist I wrote for more info:
https://gist.github.com/EdwinChua/d1546acc2a0a45bbff7804dbe22c45d6#scripting-in-c
Your answer
Follow this Question
Related Questions
Will static variables not work if negative? 1 Answer
How to make variable accessible by other scripts but not visible in editor? 1 Answer
How to get "transform.position" from Base Class in realtime 1 Answer
Data from a simple singleton / static class return null 1 Answer
getting the transform.position of a public static Transform? 1 Answer