Accessing variables from another gameobject

Hey guys,
As the search system is down I will have to ask what is probably a goofy question but here goes :wink:

Is there any way to access a GameObject’s script variables (variables I created) from another GameObject’s script?

I shall try to clairify…

I have a GameObject that I use as a game controller. I have a variable in it called GAME_STATE. I would like to be able to check that variable from all of the other GameObjects in my scene so nothing starts until the GAME_STATE changes to 1.

I would think it “should” work like this:

var GameController : GameObject;

if (GameController.GAME_STATE == 1){
PlayGame();
}

But of course it doesn’t. Any ideas?

Thanks,

-Jeremy

WARNING - The following could be completely wrong.

Is the variable you are interested in located in a script attached to the object? If so, I believe you can just set the type to the “type” of the script, then reference the variables.

var GameController : GameControllerScript; 

if (GameController.GAME_STATE == 1){ 
PlayGame(); 
}

Also, I believe you can get it by grabbing the component.

var GameController : GameObject; 

if (GameController.GetComponet(YourComponentItCanEvenBeAScriptName).GAME_STATE == 1){ 
PlayGame(); 
}

lfrog:
Yes, the variable I need is in a script called GameController attached to my GameObject.

Your first code block didn’t work at all, it said GameControllerScript is not a valid type…yada yada

I think the second one is what I need but I can’t get it to work properly either.

I found something in the script ref under GetComponent, in the code sample they provide they show a solution to my problem except for one thing. I can’t get it to work either. I tried the code as is with just the names changed, then I tried several changes, etc. I am thinking that something is left out that I am not getting.

Any ideas for something else I can try? You would think just a global (static, etc.) var would do the trick :slight_smile:

Thanks,
-Jeremy

On the first line, it should be for your case:

var GameController : GameController;

Then in the inspector, drag the script to this variable. Here is an example where I have a variable called “tile” that is the type “BoardTileScript” (the name of my script) where I am accessing a variable (rowPosition is an int) in the script directly. In this case I am passing the script to a function so it is not assigned in the inspector. I am looking for one of my scripts where I have the script assigned in the inspector, but it should work the same.

if tile.rowPosition == self.rowPosition

For the second, here is an example I from something that I am using where my script is called GameController and Component is of type Component (Perhaps I could use less confusing naming here). I am calling a method/function in my GameController script.

Controller.GetComponent(GameController).SetSeedMode()

ok, made the changes, but…

When I save (compile) the script, unity coughs and says that the best overload is not compatible with the argument list.

What am I doing wrong? Here is my code:

var GameController : GameController;

if (GameController.GetComponent(GameController).GAME_STATE == 1){
Play_Game();
}

-Jeremy

Try

var GameController : GameController; 

if (GameController.GAME_STATE == 1){ 
Play_Game(); 
}

With your GameController script dragged to the GameController variable in the inspector.

gameController can not be the same name as the class name of the script.

var gameController : GameController; 

if (gameController.GAME_STATE == 1){ 
Play_Game(); 
}

Its usually a good idea to make all variables start lower case. And all class names upper case.

Thanks guys I shall try this later today

-Jeremy

I was wondering about that when I was out Xmas shopping this afternoon. Good to know.

Update: Worked like a charm of course. Thanks guys.

I am now having an issue with Unity and the built game randomly freezing. I shall check it out a bit more tomorrow before I report anything though. It seems I can only run the game once in unity then it freezes when I try to run it again, and the built game randomly freezes. Weird.

If I still have the problem after tomorrow I shall give some more info.

EDIT: This is Unity 1.1.1 though so I should probably get 1.2 before I complain :slight_smile:

-Jeremy

New Update:

I tried it with 1.2b3 and it still freezes up Unity in the editor and in the player.

My game is very simple, I only have simple code for translating objects, score, timer, simple physics, etc.

What is happening is I can only play the game once in Unity editor, when I try to run it again Unity freezes and I have to force quit. When I build the project, my game freezes at the loadlevel call. So I commented out the entire code to change the levels and it freezes anyway.

I also tried the built game on my dad’s machine and got some really weird problems.

My Dev machine is an old eMac 800mhz with Geforce2 MX, and my dad’s machine is a dual 64bit G5 with a Geforce FX 5200. On my machine the game works perfectly graphically, but on my dad’s machine the screen went black. I tried different screen res settings, and what I got when I ran it windowed was what looked like my textures getting modifyed to some very low color palette. Very weird.

With the freezing I commented out everything new I did and still had the problem. I don’t get any errors displayed and what’s worse is it sometimes works to a point and other times it locks up before.

My code doesn’t do anything ground breaking at all.

I can’t really send my whole project as I am on dial-up but I can send whatever parts you need, or, if you really really need it I can send the whole thing.

I am trying to get this finished for a gag Christmas gift for my dad so I hope we can get it going.

Oh, one more thing that may or may not be related, before I made these major changes, I had simple code to raycast down to the ground, get the distance then move an object down (animated) until the ground and then back up. The code was run from a mouse down event. What would happen is if you clicked the mouse 6 or so times while it was still in it’s translate cycle, the game would freeze. I just changed it to not allow the script to be rerun until it is finished moving and everything was fine. It seemed weird to me that that would freeze at all.

PS: I apologize for the messed up writing, I am groggy today, I hope it is somewhat legible :slight_smile:

Thanks,
-Jeremy

99% of the time when unity locks up it is because somewhere in one of the scripts there is an infinite loop.

Eg. one error that catches me sometimes, is that i write a coroutine and i forget the yield statement in the loop.

You were right there is an endless loop somewhere in one of my scripts. I haven’t found it yet but I disabled the whole script to test and it doesn’t freeze anymore.

BUT…

Thr graphics are still screwed on my dad’s machine, the only way to see anything is to run it windowed, and then the colors go very wonky (like it’s paletted very poorly) and one other thing I forgot to mention is I only get 15 FPS on it. My slow machine running the exact same settings get’s around 70-80.

Any ideas?

-Jeremy

EDIT: I found the endless loop. I had the yield, I just had it inside an if statement :sweat_smile:

What puzzles me is the problem I was having was not consistent at all. Sometimes it would work to a point, sometimes it wouldn’t.

And if I played the game in unity and stopped it before it froze, I could carry on doing other things in unity no problem UNTIL I tried to play again. Then it would lock up and I would have to force quit.

Is it possible for an endless loop to only cause problems some of the time even though the code is accessed the exact same amount of times each time the game runs?

The only consistent problem was the graphics on my dad’s machine.

Thanks,
-Jeremy

The only reason why it would behave differently is if your if statements were checking against time or input.

What happens if you lower the pixelLightCount in the render settings for all your scenes?

In any case, you should report a bug on the graphics problem.
Would be useful to get some pictures and the project.

Yes, my code did check against time so that makes sense. Wanted to know for the next time I have a similar problem.

I tried lowering the Pixel light count with slight success. The default was 2 which didn’t work, lowered it to 1, didn’t work, then to 0 and the gameplay screen showed properly but a texture on one of my intro windows is still black.

Here are the screens you wanted:


I can’t really send you the project files as it’s over 100Mb and I am on dial-up :shock:

It puzzles me that this would run on my old crappy machine but not on my dad’s. :?

-Jeremy

I had to figure this out myself, but I made some headway. :slight_smile:

I changed the pixel light count to 0 AND changed the shaders of the objects that were getting messed from diffuse to vertexlit.

Now the textures are all fine, but the game still won’t run at 1024x768 res fullscreen. It will run at that res windowed, but all I get is a black screen and my monitor disconnects (like when you turn off the machine) when fullscreen. If I change it to 800x600 it will work fullscreen. Any reason it shouldn’t work at 1024x768? Was I using the diffuse shader incorrectly? I still have the difuse shader on some of my other objects and they run fine.

This is not a critical question as I at least have it playable now, and at a healthy 200 FPS :slight_smile:

Would just like to know why I am having issues with it.

PS: one more time the computer is a dual 64-bit G5 with GeForce FX 5200 64mb vid card.

Thanks guys,
-Jeremy