Trigger a Timer once player enters an object and stops when it enters another
I currently have this code attached to a player to start a timer that ends when I enter a trigger, but I want to make it start when I enter a trigger rather than when I start my game.
public class Timer : MonoBehaviour
public Text timerText;
private float startTime;
private bool finished = false;
private bool started = false;
void Update()
if (finished)
return;
if (started)
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
public void Finish()
finished = true;
timerText.color = Color.yellow;
void Start()
started = true;
unity3d
add a comment |
I currently have this code attached to a player to start a timer that ends when I enter a trigger, but I want to make it start when I enter a trigger rather than when I start my game.
public class Timer : MonoBehaviour
public Text timerText;
private float startTime;
private bool finished = false;
private bool started = false;
void Update()
if (finished)
return;
if (started)
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
public void Finish()
finished = true;
timerText.color = Color.yellow;
void Start()
started = true;
unity3d
add a comment |
I currently have this code attached to a player to start a timer that ends when I enter a trigger, but I want to make it start when I enter a trigger rather than when I start my game.
public class Timer : MonoBehaviour
public Text timerText;
private float startTime;
private bool finished = false;
private bool started = false;
void Update()
if (finished)
return;
if (started)
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
public void Finish()
finished = true;
timerText.color = Color.yellow;
void Start()
started = true;
unity3d
I currently have this code attached to a player to start a timer that ends when I enter a trigger, but I want to make it start when I enter a trigger rather than when I start my game.
public class Timer : MonoBehaviour
public Text timerText;
private float startTime;
private bool finished = false;
private bool started = false;
void Update()
if (finished)
return;
if (started)
float t = Time.time - startTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
public void Finish()
finished = true;
timerText.color = Color.yellow;
void Start()
started = true;
unity3d
unity3d
edited Nov 14 '18 at 7:39
derHugo
7,88231433
7,88231433
asked Nov 14 '18 at 5:28
Derrick HenickeDerrick Henicke
45
45
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You could do the same thing you've done with the bool finished
, but instead it would be something like bool started
, which returns true when the player enters the start trigger. Wrap it around the time changing code inside your Update()
statement.
Making sure the bools inside Timer are public:
public Text timerText;
public bool finished = false;
public bool started = false;
private float t = 0f;
void Update()
if (finished)
return;
if (started)
t += Time.deltaTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
void Started()
started = true;
And then inside a trigger script, reference your Timer attached to Player.
public class StartTrigger : MonoBehaviour
private GameObject player;
private Timer timer;
void Start()
player = GameObject.FindWithTag("Ellen");
if (player)
timer = player.GetComponent<Timer>();
void OnTriggerEnter(Collider other)
if (other.tag == "Ellen")
timer.Started();
I updated my new code above, but the timer doesn't start at all when I have that.
– Derrick Henicke
Nov 14 '18 at 5:46
@DerrickHenicke just to be sure, are you using OnTriggerEnter to detect triggers?
– JTizzle
Nov 14 '18 at 5:50
Yes, I have this on my triggers: public class FinishLine : MonoBehaviour private void OnTriggerEnter(Collider other) GameObject.Find("Ellen").SendMessage("Finish");
– Derrick Henicke
Nov 14 '18 at 5:50
1
If not started you should also return. Would be more efficient since doing the rest makes no sense when the timer isn't changing. AlsoFind
andSendMessage
are quite inefficient .. you should rather get the reference before hand (e.g.FindObjectOfType<Timer>();
and use the method directly
– derHugo
Nov 14 '18 at 5:56
1
@DerrickHenicke I mean: changeif (started) float t = Time.time - startTime;
toif(!started) return; float t = Time.time - startTime;
and get the reference to the Timer before e.g.public Timer _timer; private void Awake() _timer = FindObjectOfType<Timer>();
or reference it in the inspector and later only use_timer.Started()
and_timer.Finished()
– derHugo
Nov 14 '18 at 6:09
|
show 11 more comments
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293704%2ftrigger-a-timer-once-player-enters-an-object-and-stops-when-it-enters-another%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could do the same thing you've done with the bool finished
, but instead it would be something like bool started
, which returns true when the player enters the start trigger. Wrap it around the time changing code inside your Update()
statement.
Making sure the bools inside Timer are public:
public Text timerText;
public bool finished = false;
public bool started = false;
private float t = 0f;
void Update()
if (finished)
return;
if (started)
t += Time.deltaTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
void Started()
started = true;
And then inside a trigger script, reference your Timer attached to Player.
public class StartTrigger : MonoBehaviour
private GameObject player;
private Timer timer;
void Start()
player = GameObject.FindWithTag("Ellen");
if (player)
timer = player.GetComponent<Timer>();
void OnTriggerEnter(Collider other)
if (other.tag == "Ellen")
timer.Started();
I updated my new code above, but the timer doesn't start at all when I have that.
– Derrick Henicke
Nov 14 '18 at 5:46
@DerrickHenicke just to be sure, are you using OnTriggerEnter to detect triggers?
– JTizzle
Nov 14 '18 at 5:50
Yes, I have this on my triggers: public class FinishLine : MonoBehaviour private void OnTriggerEnter(Collider other) GameObject.Find("Ellen").SendMessage("Finish");
– Derrick Henicke
Nov 14 '18 at 5:50
1
If not started you should also return. Would be more efficient since doing the rest makes no sense when the timer isn't changing. AlsoFind
andSendMessage
are quite inefficient .. you should rather get the reference before hand (e.g.FindObjectOfType<Timer>();
and use the method directly
– derHugo
Nov 14 '18 at 5:56
1
@DerrickHenicke I mean: changeif (started) float t = Time.time - startTime;
toif(!started) return; float t = Time.time - startTime;
and get the reference to the Timer before e.g.public Timer _timer; private void Awake() _timer = FindObjectOfType<Timer>();
or reference it in the inspector and later only use_timer.Started()
and_timer.Finished()
– derHugo
Nov 14 '18 at 6:09
|
show 11 more comments
You could do the same thing you've done with the bool finished
, but instead it would be something like bool started
, which returns true when the player enters the start trigger. Wrap it around the time changing code inside your Update()
statement.
Making sure the bools inside Timer are public:
public Text timerText;
public bool finished = false;
public bool started = false;
private float t = 0f;
void Update()
if (finished)
return;
if (started)
t += Time.deltaTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
void Started()
started = true;
And then inside a trigger script, reference your Timer attached to Player.
public class StartTrigger : MonoBehaviour
private GameObject player;
private Timer timer;
void Start()
player = GameObject.FindWithTag("Ellen");
if (player)
timer = player.GetComponent<Timer>();
void OnTriggerEnter(Collider other)
if (other.tag == "Ellen")
timer.Started();
I updated my new code above, but the timer doesn't start at all when I have that.
– Derrick Henicke
Nov 14 '18 at 5:46
@DerrickHenicke just to be sure, are you using OnTriggerEnter to detect triggers?
– JTizzle
Nov 14 '18 at 5:50
Yes, I have this on my triggers: public class FinishLine : MonoBehaviour private void OnTriggerEnter(Collider other) GameObject.Find("Ellen").SendMessage("Finish");
– Derrick Henicke
Nov 14 '18 at 5:50
1
If not started you should also return. Would be more efficient since doing the rest makes no sense when the timer isn't changing. AlsoFind
andSendMessage
are quite inefficient .. you should rather get the reference before hand (e.g.FindObjectOfType<Timer>();
and use the method directly
– derHugo
Nov 14 '18 at 5:56
1
@DerrickHenicke I mean: changeif (started) float t = Time.time - startTime;
toif(!started) return; float t = Time.time - startTime;
and get the reference to the Timer before e.g.public Timer _timer; private void Awake() _timer = FindObjectOfType<Timer>();
or reference it in the inspector and later only use_timer.Started()
and_timer.Finished()
– derHugo
Nov 14 '18 at 6:09
|
show 11 more comments
You could do the same thing you've done with the bool finished
, but instead it would be something like bool started
, which returns true when the player enters the start trigger. Wrap it around the time changing code inside your Update()
statement.
Making sure the bools inside Timer are public:
public Text timerText;
public bool finished = false;
public bool started = false;
private float t = 0f;
void Update()
if (finished)
return;
if (started)
t += Time.deltaTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
void Started()
started = true;
And then inside a trigger script, reference your Timer attached to Player.
public class StartTrigger : MonoBehaviour
private GameObject player;
private Timer timer;
void Start()
player = GameObject.FindWithTag("Ellen");
if (player)
timer = player.GetComponent<Timer>();
void OnTriggerEnter(Collider other)
if (other.tag == "Ellen")
timer.Started();
You could do the same thing you've done with the bool finished
, but instead it would be something like bool started
, which returns true when the player enters the start trigger. Wrap it around the time changing code inside your Update()
statement.
Making sure the bools inside Timer are public:
public Text timerText;
public bool finished = false;
public bool started = false;
private float t = 0f;
void Update()
if (finished)
return;
if (started)
t += Time.deltaTime;
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + ":" + seconds;
void Started()
started = true;
And then inside a trigger script, reference your Timer attached to Player.
public class StartTrigger : MonoBehaviour
private GameObject player;
private Timer timer;
void Start()
player = GameObject.FindWithTag("Ellen");
if (player)
timer = player.GetComponent<Timer>();
void OnTriggerEnter(Collider other)
if (other.tag == "Ellen")
timer.Started();
edited Nov 14 '18 at 7:41
derHugo
7,88231433
7,88231433
answered Nov 14 '18 at 5:38
JTizzleJTizzle
1027
1027
I updated my new code above, but the timer doesn't start at all when I have that.
– Derrick Henicke
Nov 14 '18 at 5:46
@DerrickHenicke just to be sure, are you using OnTriggerEnter to detect triggers?
– JTizzle
Nov 14 '18 at 5:50
Yes, I have this on my triggers: public class FinishLine : MonoBehaviour private void OnTriggerEnter(Collider other) GameObject.Find("Ellen").SendMessage("Finish");
– Derrick Henicke
Nov 14 '18 at 5:50
1
If not started you should also return. Would be more efficient since doing the rest makes no sense when the timer isn't changing. AlsoFind
andSendMessage
are quite inefficient .. you should rather get the reference before hand (e.g.FindObjectOfType<Timer>();
and use the method directly
– derHugo
Nov 14 '18 at 5:56
1
@DerrickHenicke I mean: changeif (started) float t = Time.time - startTime;
toif(!started) return; float t = Time.time - startTime;
and get the reference to the Timer before e.g.public Timer _timer; private void Awake() _timer = FindObjectOfType<Timer>();
or reference it in the inspector and later only use_timer.Started()
and_timer.Finished()
– derHugo
Nov 14 '18 at 6:09
|
show 11 more comments
I updated my new code above, but the timer doesn't start at all when I have that.
– Derrick Henicke
Nov 14 '18 at 5:46
@DerrickHenicke just to be sure, are you using OnTriggerEnter to detect triggers?
– JTizzle
Nov 14 '18 at 5:50
Yes, I have this on my triggers: public class FinishLine : MonoBehaviour private void OnTriggerEnter(Collider other) GameObject.Find("Ellen").SendMessage("Finish");
– Derrick Henicke
Nov 14 '18 at 5:50
1
If not started you should also return. Would be more efficient since doing the rest makes no sense when the timer isn't changing. AlsoFind
andSendMessage
are quite inefficient .. you should rather get the reference before hand (e.g.FindObjectOfType<Timer>();
and use the method directly
– derHugo
Nov 14 '18 at 5:56
1
@DerrickHenicke I mean: changeif (started) float t = Time.time - startTime;
toif(!started) return; float t = Time.time - startTime;
and get the reference to the Timer before e.g.public Timer _timer; private void Awake() _timer = FindObjectOfType<Timer>();
or reference it in the inspector and later only use_timer.Started()
and_timer.Finished()
– derHugo
Nov 14 '18 at 6:09
I updated my new code above, but the timer doesn't start at all when I have that.
– Derrick Henicke
Nov 14 '18 at 5:46
I updated my new code above, but the timer doesn't start at all when I have that.
– Derrick Henicke
Nov 14 '18 at 5:46
@DerrickHenicke just to be sure, are you using OnTriggerEnter to detect triggers?
– JTizzle
Nov 14 '18 at 5:50
@DerrickHenicke just to be sure, are you using OnTriggerEnter to detect triggers?
– JTizzle
Nov 14 '18 at 5:50
Yes, I have this on my triggers: public class FinishLine : MonoBehaviour private void OnTriggerEnter(Collider other) GameObject.Find("Ellen").SendMessage("Finish");
– Derrick Henicke
Nov 14 '18 at 5:50
Yes, I have this on my triggers: public class FinishLine : MonoBehaviour private void OnTriggerEnter(Collider other) GameObject.Find("Ellen").SendMessage("Finish");
– Derrick Henicke
Nov 14 '18 at 5:50
1
1
If not started you should also return. Would be more efficient since doing the rest makes no sense when the timer isn't changing. Also
Find
and SendMessage
are quite inefficient .. you should rather get the reference before hand (e.g. FindObjectOfType<Timer>();
and use the method directly– derHugo
Nov 14 '18 at 5:56
If not started you should also return. Would be more efficient since doing the rest makes no sense when the timer isn't changing. Also
Find
and SendMessage
are quite inefficient .. you should rather get the reference before hand (e.g. FindObjectOfType<Timer>();
and use the method directly– derHugo
Nov 14 '18 at 5:56
1
1
@DerrickHenicke I mean: change
if (started) float t = Time.time - startTime;
to if(!started) return; float t = Time.time - startTime;
and get the reference to the Timer before e.g. public Timer _timer; private void Awake() _timer = FindObjectOfType<Timer>();
or reference it in the inspector and later only use _timer.Started()
and _timer.Finished()
– derHugo
Nov 14 '18 at 6:09
@DerrickHenicke I mean: change
if (started) float t = Time.time - startTime;
to if(!started) return; float t = Time.time - startTime;
and get the reference to the Timer before e.g. public Timer _timer; private void Awake() _timer = FindObjectOfType<Timer>();
or reference it in the inspector and later only use _timer.Started()
and _timer.Finished()
– derHugo
Nov 14 '18 at 6:09
|
show 11 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53293704%2ftrigger-a-timer-once-player-enters-an-object-and-stops-when-it-enters-another%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown