- Home /
Shared variable problem, between scripts, help?
So, I am accessing variables in other scripts via unityscript. And since unityscript does not return variables to otherscripts, I have to manually program it. They are fighting eachother for the variables, or if I can get some to update one way, they wont retro update from original script, because the other script is correcting it.
Script 1 should be able to edit both variables (someint1) and (someint2), script 2 should be able to edit 1 variable (invariable1, script 3 should be able to edit 1 variable (intvariable2).
Note that basically, someint1 = invariable1, and someint2 = intvariable2.
The way I have it set up is
Script 1
var someint1 : int;
var someint2 : int;
function Start()
someint1 = somenumber;
someint2 = somenumber;
function Lateupdate()
someint1 = GetObject.Find(GameObject1).GetComponent(GameComponent1).intvariable1;
someint2 = GetObject.Find(GameObject2).GetComponent(GameComponent2).intvariable2;
Script2
var intvariable1 : int;
function Update()
intvariable1 = GetObject.Find(GameObject3).GetComponent(GameComponent3).someint1;
Script3
var intvariable2 : int;
function Update()
intvariable2 = GetObject.Find(GameObject3).GetComponent(GameComponent3).someint2;
Answer by whydoidoit · Jan 25, 2013 at 12:03 AM
Well you are asking it to update all over the place - what are you trying to do? You should have one source of the "truth" about a value and always read and write it there. Also you should not do those Find and GetComponent in an Update call - cache the component reference in Start or Awake (or assign the references to the game objects using the inspector).
The variables get edited in different functions in each script, so So I can't have one "truth". And if I put them @ function Start, then they do not update when edited. I need to make it so they can all do this without interfering with each other. How can I let them be edited in different scripts?
GameComponent3 gc3;
function Start()
{
gc3 = GameObject.Find("GameObject3").GetComponent(GameComponent3);
}
function Update()
{
gc3.someint2 += 20;
}
if I do not have the other scripts access the variables, basically getting them back, then the scripts end up with different ideas of what the variables should be. When editing in one script, the value doesnt return to original script, so that is why I do it that way. C# will return the variable, but not unityscript.
@whydoidoit Or are you meaning something else? I don't quite get what your saying with that code. GameComponent3 gc3; but then you have GameComponent3 and gc3 separated..?
maybe a sendmessage approach would be more appropriate? Send message, telling script2 & script3 to perform function to edit the variable, ins$$anonymous$$d of editing/accessing variable in script1.
I would like to try to keep it how I have. But it to work all the way. So if theres a way...
I tried to test your way, but I get error: cannot convert 'gamecomponent' to int.
Edit: just had to change the vars from var asdf : int; to var asdf;. But now it is saying unknown identifier for the variables I am trying to access.
Edit2: Never$$anonymous$$d I had a typo... testing now.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to access .int variable? 1 Answer
Get object, which is more optimized? 1 Answer
pickup weapon with GetComponent 0 Answers