- Home /
Question by
OiEuSouOGah · Jul 10, 2020 at 01:43 PM ·
transformawake
Is Awake() safe to start variables using transform's information?
Hi. I'm starting unity and just learned about Awake(). It seems to be a good place to start variables.
I made a script for projectiles. I want my projectile to know the player's position when it is instantiated.
First, can I use the player's transform position in Awake() with no problems or should I do that in Start() instead?
public class Projectile : MonoBehaviour
{
private Vector3 player_position;
void Awake()
{
player_position = GameObject.FindWithTag("Player").transform.position; // is this okay?
}
}
Second, if I were to do operations with the transform, can I do it safely on Awake() or just in Start()?
public class Projectile : MonoBehaviour
{
private Vector3 player_position;
private Vector3 target;
void Awake()
{
player_position = GameObject.FindWithTag("Player").transform.position;
target = (player_position - transform.position); // is this okay(2)?
}
}
Comment
Your answer
Follow this Question
Related Questions
Questions about UGUI Rect Transform Anchors. How to make UI objects scale, correctly, with screen? 1 Answer
Send array of transforms via RPC. [C#] 1 Answer
Stop transform position moving objects together 1 Answer
Perpendicular forward :-) 0 Answers
Transforming a GameObject with an unattached script 1 Answer