Variable of a script "fails" the other

Good for all. Since completing the first phase of my program, how they have not given me new instructions and my mom is ending the sheets where I will base my stats (when is the result better understood), I decided to make a record of consultations, if the doctor want to save the patient consultation, include observations, etc…
Anyway, what happens to me is I have this code (fragment) in the script calculator.js

if(resultH >= 1){
	consult="El paciente tiene "+resultH+" horas de vida o "+yyyyInt+" años.";
	GUI.Label(Rect(Screen.width/2-column4Pos, Screen.height/2-230,Screen.width/2-100,100), consult);
	GUI.Label(Rect(Screen.width/2-column4Pos, Screen.height/2-120,Screen.width/2-100,100), key);
	}

That if validates and verifies certain data, in this case, it overwrites a string variable, which will be displayed in the Label beneath: P Good thing it perfect, in fact seen in the inspector that changes, is printed with a print () to verify that this has been written, but over the script reg.js, which is creating the record in the log file … SO fails when there is: (
This is the code reg.js (clarified I tried removing the # pragma strict just in case and there was no difference

#pragma strict
var statusAddReg: boolean = false;
/*	Variable que guarda el ID de cada paciente	*/
private var ID: int;
/*	GetComponent	*/
public var otherGameObject:  GameObject;
private var anotherScript: Calculator;
function Awake(){
anotherScript = otherGameObject.gameObject.GetComponent("Calculator") as Calculator; 
}
 
/*	Script	*/
function Update () {
	Invoke("addReg", 0.1);
}

function addReg () {
	ID = PlayerPrefs.GetInt("ID");
	if(statusAddReg){
	print(anotherScript.consult);
	ID+=1;
	PlayerPrefs.SetString(anotherScript.namePacient+"_"+ID, ""+anotherScript.namePacient+" | Resultado de la consulta: "+anotherScript.consult+"");
	PlayerPrefs.SetInt("ID", ID);
	statusAddReg=false;
	}
}

He explained that the variable statusAddReg [/ u] is activated from the calculator.js [/ b], I noticed and it does, but it gives me that data is lost in the middle … I explain … if I put a print (anotherScript.consult) print what I have to print, that is (for example):
**__ __**The patient has 122,640 hours of life or 14 years.**__ __**
Now, if I put that print () inside the if (in the script reg.js):
**__ __**if(statusAddReg){ // Code }**__ __**
The print does not return nothing … how to consult if the variable does not have anything inside (does not return NULL simply returns nothing xD).
Is there any solution for this? Thanks to all

This makes absolutely no sense:

function Update () {
    Invoke("addReg", 0.1);
}

Execute it in every frame, but with a delay of 0.1 seconds? Why? This may end up being executed more than once per frame and than not at all. It just makes no sense to me.

Instead execute it directly:

function Update () {
    addReg ();
}

Don’t know if that is the cause for your issue, but it clearly makes no sense to me.

yes, you have right, but the idea is using InvokeRepeating xD, is a little error :stuck_out_tongue:
ok… some have any idea of this issue?

statusAddReg is not set to true. It is as simple as that. Make sure it is set.

Yes, with other script swich statusAddReg to TRUE, but the print()… prints nothing xD

You are not showing the relevant code here where statusAddReg is set to true. How are we supposed to help like that? Maybe you are right and it is set, but for some reason it still doesn’t work. We can only help with actual code.

Calculator.js (important fragment)

/*	Script	*/
function OnGUI () {
	// Convierto el mes en número a nombre.
	if(month == 1){nameOfMonth="Enero";}
	if(month == 2){nameOfMonth="Febrero";}
	if(month == 3){nameOfMonth="Marzo";}
	if(month == 4){nameOfMonth="Abril";}
	if(month == 5){nameOfMonth="Mayo";}
	if(month == 6){nameOfMonth="Junio";}
	if(month == 7){nameOfMonth="Julio";}
	if(month == 8){nameOfMonth="Agosto";}
	if(month == 9){nameOfMonth="Septiembre";}
	if(month == 10){nameOfMonth="Octubre";}
	if(month == 11){nameOfMonth="Noviembre";}
	if(month == 12){nameOfMonth="Diciembre";}
	// Listo
GUI.skin = guifile;
GUI.Box(Rect(Screen.width/2-300,Screen.height/2-300,600,600), "Hoy es "+day+" de "+nameOfMonth+" del "+year+".");
	dd = GUI.TextField (Rect (Screen.width/2-column1Pos, Screen.height/2-230, 50, 40), dd, 2);
	mm = GUI.TextField (Rect (Screen.width/2-column2Pos, Screen.height/2-230, 50, 40), mm, 2);
	yyyy = GUI.TextField (Rect (Screen.width/2-column3Pos, Screen.height/2-230, 70, 40), yyyy, 4);
		if(GUI.Button(Rect(Screen.width/2-column1Pos,Screen.height/2-100,120,47), "Calcular")){
		Invoke("Calculator", 0.1);
		}
	if(resultH >= 1){
	consult="El paciente tiene "+resultH+" horas de vida o "+yyyyInt+" años.";
	GUI.Label(Rect(Screen.width/2-column4Pos, Screen.height/2-230,Screen.width/2-100,100), consult);
	GUI.Label(Rect(Screen.width/2-column4Pos, Screen.height/2-120,Screen.width/2-100,100), key);
	}
	statusName = GUI.Toggle(Rect(Screen.width/2-column1Pos, Screen.height/2-180, 180, 30), statusName, "  Guardar consulta");
	if(statusName){
	namePacient = GUI.TextField (Rect (Screen.width/2-column1Pos, Screen.height/2-150, 175, 35), namePacient, 25);
	}
	/*		*/
}
//
function Calculator () {
	if(statusName){
	anotherScript.statusAddReg=true;
	}
	// Sobrescribo las variables anteriormente escritas con las variables DD, MM y YYYY :smile:
	ddInt = parseInt (dd);
	mmInt = parseInt (mm);
	yyyyInt = parseInt (yyyy);
	//
	// Calculo los años
	yyyyInt=year-yyyyInt;
	resultH = yyyyInt*8760;
	// Listo
}

reg.js

#pragma strict
var statusAddReg: boolean = false;
/*	Variable que guarda el ID de cada paciente	*/
private var ID: int;
/*	GetComponent	*/
public var otherGameObject:  GameObject;
private var anotherScript: Calculator;
function Awake(){
anotherScript = otherGameObject.gameObject.GetComponent("Calculator") as Calculator; 
}
 
/*	Script	*/
function Update () {
	addReg ();
}

function addReg () {
	ID = PlayerPrefs.GetInt("ID");
	if(statusAddReg){
	print(anotherScript.consult);
	ID+=1;
	PlayerPrefs.SetString(anotherScript.namePacient+"_"+ID, ""+anotherScript.namePacient+" | Resultado de la consulta: "+anotherScript.consult+"");
	PlayerPrefs.SetInt("ID", ID);
	statusAddReg=false;
	}
}

Sorry for the double posting but i can’t edit the last message, if press “EDIT POST” the textarea appears in white :open_mouth: LOL
Ok, the Calculator.js calculates some variables and activate statusAddReg if toggle is pressed, in this case, is PRESSED, really :stuck_out_tongue:
Thanks for the support :smile:

Where is Calculator () called? Because in there you set statusAddReg to true.

Calculator() is Invoke in:

if(GUI.Button(Rect(Screen.width/2-column1Pos,Screen.height/2-100,120,47), "Calcular")){
		Calculator();
		}

Do you have any error messages in the console?

It can not be… I just reopen Unity and simply started to work!
Could be a problem in Unity, or has dropped a divine being and has repaired this? xD
Anyway, thanks for the replies, sorry for wasting your time, I can not really believe it just works as if nothing

again, fails, the errors depends of recently changes of reg.js :stuck_out_tongue:

#pragma strict
public var statusAddReg: boolean = false;
/*	GetComponent	*/
public var otherGameObject:  GameObject;
private var anotherScript: Calculator;
function Awake(){
anotherScript = otherGameObject.gameObject.GetComponent("Calculator") as Calculator; 
}
 
/*	Script	*/
function Update () {
	if(statusAddReg){
	addReg ();
	}
}

function addReg () {
	var old_registry = PlayerPrefs.GetString("registry");
	print(anotherScript.consult);
	PlayerPrefs.SetString("registry", old_registry+"\n"+anotherScript.namePacient+" ("+anotherScript.day+"/"+anotherScript.month+"/"+anotherScript.year+"):"+"\nResultado de la consulta: "+anotherScript.consult+"\nObservaciones: "+anotherScript.observations+"\n\n");
	statusAddReg=false;
}

If delete this line:

statusAddReg=false;

The script works, but I need that variable to stop writing playerprefs because if not stop, just keep writing, creating me a “list” of things repeated… seems that the script does not reach completion and the variable is already passes false (or something). How do I get that variable is set false and “finished” writing the playerprefs completely?

Once more the solution is very simple, you are not setting statusAddReg to true or better, that script is not executed properly. Or alternatively you set it to false again.
You may test it pretty easily by using a debug log at the place where you set it to true.

I delete this line in reg.js

statusAddReg=false;

but i can’t delete the line statusAddReg=false; why without this line, the playerprefs save every frame with extra data, and i not need this, i not need text over and over again :stuck_out_tongue:

This variable (statusAddReg) is previously enabled with (in script Calculator.js)

if(statusName){
	anotherScript.statusAddReg=true;
	}

And yes, statusName is True in this case.

Any solution? Basicaly, i need finish the funcion addReg() and swich statusAddReg to false. Update to follow then checking that variable, and if true, call back to Addreg(), but if not, no…

Instead of calling

anotherScript.statusAddReg=true;

you may directly call

anotherScript.addReg ();

Like that you can also get rid of the Update.

great, but the script can’t write in playerprefs… is it a problem of velocity? why the script reg.js run and print but can’t write in the playerprefs. This issue is for… why?
Actual code is:
Calculator.js (important fragment):

/*	Mis comentarios:	
Un año equivale a 8760 horas	*/
#pragma strict
import System;
var guifile: GUISkin;
	
	// [...]
	
/*	Key	*/
public var key: String; // Clave del paciente, según las horas de vida

/*	GetComponent	*/
public var otherGameObject:  GameObject;
private var anotherScript: reg;
function Awake(){
 anotherScript = otherGameObject.gameObject.GetComponent("reg") as reg; 
 }
}
// [...]
function Calculator () {
	if(statusName){
	anotherScript.addReg();
	}
  // [...]
}

[B]reg.js[/B] (all script)
[code]/*	Script creado por Santiago Agustín Gimenez el día 11 de Diciembre del 2013.
	http://santigimenez.com.ar	*/
#pragma strict
public var statusAddReg: boolean = false;
/*	GetComponent	*/
public var otherGameObject:  GameObject;
private var anotherScript: Calculator;
function Awake(){
anotherScript = otherGameObject.gameObject.GetComponent("Calculator") as Calculator; 
}
 
/*	Script	*/
function addReg () {
	var old_registry = PlayerPrefs.GetString("registry");
	print("Consulta: "+anotherScript.consult);
	PlayerPrefs.SetString("registry", old_registry+"\n"+anotherScript.namePacient+" ("+anotherScript.day+"/"+anotherScript.month+"/"+anotherScript.year+"):"+"\nClave: "+anotherScript.key+"\nResultado de la consulta: "+anotherScript.consult+"\nObservaciones: "+anotherScript.observations+"\n\n");
}

sorry for the big inconvenience :frowning:

Sorry for the double posting, i can’t edit the last message…

great, but the script can’t write in playerprefs… is it a problem of velocity? why the script reg.js run and print but can’t write in the playerprefs. This issue is for… why?
Actual code is:
Calculator.js (important fragment):

/* Mis comentarios:
Un año equivale a 8760 horas */
#pragma strict
import System;
var guifile: GUISkin;

// [...]

/* Key */
public var key: String; // Clave del paciente, según las horas de vida

/* GetComponent */
public var otherGameObject: GameObject;
private var anotherScript: reg;
function Awake(){
anotherScript = otherGameObject.gameObject.GetComponent("reg") as reg;
}
}
// [...]
function Calculator () {
if(statusName){
anotherScript.addReg();
}
// [...]
}

reg.js (all script)

/*	Script creado por Santiago Agustín Gimenez el día 11 de Diciembre del 2013.
	http://santigimenez.com.ar	*/
#pragma strict
public var statusAddReg: boolean = false;
/*	GetComponent	*/
public var otherGameObject:  GameObject;
private var anotherScript: Calculator;
function Awake(){
anotherScript = otherGameObject.gameObject.GetComponent("Calculator") as Calculator; 
}
 
/*	Script	*/
function addReg () {
	var old_registry = PlayerPrefs.GetString("registry");
	print("Consulta: "+anotherScript.consult);
	PlayerPrefs.SetString("registry", old_registry+"\n"+anotherScript.namePacient+" ("+anotherScript.day+"/"+anotherScript.month+"/"+anotherScript.year+"):"+"\nClave: "+anotherScript.key+"\nResultado de la consulta: "+anotherScript.consult+"\nObservaciones: "+anotherScript.observations+"\n\n");
}

sorry for the big inconvenience

The confusion you are experiencing is screaming for a refactoring.

why…?