Can somebody tell me what the hell is wrong in my code?
If i only check one time interval with one “if”, works fine but if i put the others only works the last one.
Please i’m going crazy!!!
The code only have to change every 5 secs the text ot a 3d text object
private var t1 : float;
private var tmp : TextMesh ;
function Start(){
t1=Time.time;
tmp=gameObject.GetComponent(TextMesh);
}
function Update(){
Comprueba_tiempo();
}
function CambiaTexto (tx : String) {
Debug.Log(tx + "Si CambiaTexto a " + (Time.time - t1));
tmp.text = tx;
}
function Comprueba_tiempo (){
if (Time.time - t1 > 5 && Time.time - t1 < 10){
Debug.Log("Lo muestra mal");
CambiaTexto ("Una produccion de Daniel Designs");}
else{
CambiaTexto("mal");
}
if (Time.time - t1 > 15 && Time.time - t1 < 20){
CambiaTexto ("Basada en la película CAZA SALVAJE(Tambien de Daniel Designs");}
else{
CambiaTexto("");
}
if (Time.time - t1 > 25 && Time.time - t1 < 30){
CambiaTexto ("Diseño de Daniel Rey");}
else{
CambiaTexto("");
}
if (Time.time - t1 > 35 && Time.time - t1 < 40){
CambiaTexto ("Gracias a Unity 3D");}
else{
CambiaTexto("");
}
if (Time.time - t1 > 45 && Time.time - t1 < 50){
CambiaTexto ("Con la financiacion de G. Matilde...");}
else{
CambiaTexto("");
}
if (Time.time - t1 > 55 && Time.time - t1 <60){
CambiaTexto ("...y M. Peleteiro");}
else{
CambiaTexto("");
}
}
You’re right in that you need an else if statment. I don’t know too much javaScript but I put the together. I also added a helper script to make your time test easier.
private var t1 : float;
private var tmp : TextMesh;
function Start(){
t1=Time.time;
// tmp=gameObject.GetComponent(TextMesh);
}
function Update(){
Comprueba_tiempo2();
}
function CambiaTexto (tx : String) {
Debug.Log(tx + "Si CambiaTexto a " + (Time.time - t1));
// tmp.text = tx;
}
function Comprueba_tiempo (){
if (Time.time - t1 > 5 && Time.time - t1 < 10){
Debug.Log("Lo muestra mal");
CambiaTexto ("Una produccion de Daniel Designs");
}else if (Time.time - t1 > 15 && Time.time - t1 < 20){
CambiaTexto ("Basada en la película CAZA SALVAJE(Tambien de Daniel Designs");
}else if (Time.time - t1 > 25 && Time.time - t1 < 30){
CambiaTexto ("Diseño de Daniel Rey");
}else if (Time.time - t1 > 35 && Time.time - t1 < 40){
CambiaTexto ("Gracias a Unity 3D");
}else if (Time.time - t1 > 45 && Time.time - t1 < 50){
CambiaTexto ("Con la financiacion de G. Matilde...");
}else if (Time.time - t1 > 55 && Time.time - t1 <60){
CambiaTexto ("...y M. Peleteiro");
}else{
CambiaTexto("");
// } else {
// CambiaTexto("mal");
// }
}
}
function Comprueba_tiempo2 (){
if (timeTester ( 5.0f, 10.0f)){
// Debug.Log("Lo muestra mal");
CambiaTexto ("Una produccion de Daniel Designs");
}else if (timeTester ( 15.0f, 20.0f)){
CambiaTexto ("Basada en la película CAZA SALVAJE(Tambien de Daniel Designs");
}else if (timeTester ( 25.0f, 30.0f)){
CambiaTexto ("Diseño de Daniel Rey");
}else if (timeTester ( 35.0f, 40.0f)){
CambiaTexto ("Gracias a Unity 3D");
}else if (timeTester ( 45.0f, 50.0f)){
CambiaTexto ("Con la financiacion de G. Matilde...");
}else if (timeTester ( 55.0f, 60.0f)){
CambiaTexto ("...y M. Peleteiro");
}else{
CambiaTexto("");
// } else {
// CambiaTexto("mal");
// }
}
}
function timeTester ( low : float, high : float) {
if (Time.time - t1 > low && Time.time - t1 < high) {
return true;
} else {
return false;
}
}