- Home /
Question by
edufissure · Jan 27, 2019 at 08:30 PM ·
scripting problemcoroutinedelaydestroy object
DisplayText info and wait for animation ends to destroyobject ( time extra box)
///using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
// A grenade
// - instantiates a explosion Prefab when hitting a surface
// - then destroys itself
public class TriggeredTimeBox : MonoBehaviour
{
//You can set this in a prefab: dont be confused and use two at at time
//public int idMonolith;
//public int experienceForthisMonolith;
//public string clue;
private bool havebeenVisited;
private Animator _anim;
public Text displayInfo;
private string sDisplayInfo;
/* Usually Awake() is used to initialize if certain values or script are dependent on each other and would cause errors if one of them
is initialized too late (awake runs before the game starts). Awake is also called only once for every script instance.
Codes in Start() function get executed only if the script is activated while, codes in Awake() function get executed even if the script is not activated. Try it !
*/
void Awake()
{
havebeenVisited = false;
//customImage.enabled = false;
sDisplayInfo = "";
displayInfo.enabled= false;
_anim = GetComponent<Animator>();
}
private IEnumerator OnTriggerEnter(Collider other)
{
PlayerCharacterManager player= other.GetComponent<PlayerCharacterManager>();
//Check if other object is PlayerCharacter
//if( havebeenVisited == false) havebeenVisited=true;
float remainingTime = player.getRemainingTime();
float maxTime = player.getMaxTime();
sDisplayInfo = "";
if( player!= null) {
if(!havebeenVisited)
{
//At least we add 10 seconds of time
float timeToAdd = Mathf.Round (Random.Range(10.0f, maxTime));
//Condition: at least add one point of health, if HealthToAdd is zero, then assign at least 1.0f
//if (HealthToAdd <= 0) HealthToAdd=1.0f;
Debug.Log("Time to add : " + timeToAdd);
displayInfo.enabled = true;
havebeenVisited=true;
Debug.Log("Time increased : " + timeToAdd);
player.addTime(timeToAdd);
sDisplayInfo = "Time increased + " + timeToAdd.ToString() + " seconds";
displayInfo.text = sDisplayInfo;
_anim.SetBool( "PlayOpenAnimation", true);
float timeAnim = _anim.GetCurrentAnimatorStateInfo(0).length;
//_animator.SetInteger("gameResult",1);
yield return new WaitForSeconds(timeAnim);
StartCoroutine(Delay());
}
//displayInfo.text = sDisplayInfo;
} //End player
}
//Not used as character cant enter in monolith....
private void OnTriggerStay(Collider other)
{
}
private void OnTriggerExit(Collider other)
{
// ContactPoint contact = collision.contacts[0];
// Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
//Debug.Log("Ya no hay tema bfff");
// Vector3 pos = contact.point;
// Instantiate(explosionPrefab, pos, rot);
// Destroy(gameObject);
// customImage.enabled= false;
// displayInfo.enabled = false;
}
private IEnumerator Delay()
{
Debug.Log("Delay Time Box");
print(Time.time);
yield return new WaitForSeconds(1.0f);
print(Time.time);
displayInfo.enabled = false;
if ( havebeenVisited) Destroy(this.gameObject);
//Destroy(this.gameObject,2.0f);
}
}
Please help dont know why isnt working... After animation stops, stills one or two seconds the message and the treasure get in the game
This is a TimeBox, where you can add some random time to your remaining time. I need to show the info, display text for at least 3.0 seconds, enough time to read it, and then or at same time play animation where the treasure chest is moving and rotating before dissapearing....
I ve been working all the afternoon and i cant get the key... any help would be apreciated....
Comment
Your answer
Follow this Question
Related Questions
Function delay? 1 Answer
[HELP] How to make camera smooth change position while follow object? 1 Answer
Endless runner background looping delayed. 0 Answers
How do I delay an Ad? 2 Answers