Questions about unity js syntax

Is it possible to define gameobjects otuside a code and then use them in other scirpts like :

private var examplevar : GameObject;
examplevar = GameObject.Find("Something That i Dont Understand-Where do you define THIS?");

also is GameObject.FindWithTag faster than simply GameObject.Find
Thanks in advance :slight_smile:

GameObject.Find (name : String); looks of the name of the object and depending on where alphabetically it is may be hair slower depending on how many tagged items you have. Either way is too fast to worry about though lol.

Yes you can declare a variable outside of a function in Java/Unityscript.

Make it public, not private. http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

–Eric

Yes indeed you were helpful thanks. Only one question though is it possible to define the gameobject in another scirpt and then gameobject.find it within another script impying that all those scirpts are in one system.

Thank you, you were helpful! Though i have one last question if for example we have

var Something :Gameobject;
Something = Gameobject.find("This here");

Do i have to define “This Here” somewhere or is it just a string name that is going in the prefab. Because im really dazzled about the .find part simply because the gameobject “Something” doesnt have anything to do with “This here”. Do you need to declare “This here” somewhere or is it just a blanc gameobject which you can later (in the code) attach a scirpt to?

GameObject.Find. “Finds a game object by name and returns it. If no game object with name can be found, null is returned. If name contains a ‘/’ character it will traverse the hierarchy like a path name. This function only returns active gameobjects.”

–Eric

As Eric says above the GameObject.Find(" ") will find any active game object in your scene with the same name as you enter in the " "

However it is worth noting that if you know which game object you are connecting to the variable, then you can just drag the Gameobject on to the Something value of the script in the inspecter.

Using GameObject.Find is quite expensive, more so if you are doing it a lot each frame.

Yes i enderstand that, but what i dont is with var Something :Gameobject; you create the gameobject right? and then Something = Gameobject.find("This here"); “Something” is the game object ok, but the “This here” is the i dont get part…

No - this doesn’t create a GameObject - it simply declares a variable that will, eventually, point to a GameObject.

What’s not to get? “This here” is the name of the GameObject in the current scene that you want to find. The result is then assigned to the variable called Something.

Okay but how do you create a gameobject in code? Will it be a .prefab file or?

var player : GameObject;
player = new GameObject ("Player");
player.AddComponent ("Rigidbody");
player.AddComponent ("BoxCollider");

I understand that but is it possible to operate without the player = new GameObject ("Player")
And if not could you store those values in an .prefab file and then call those values from that .prefab file using the GameObject.find();

And finally is there another way to define a GameObject apart from the new GameObject("BLABLA")

Im trying to understand this basiclly because a friend of mine created a system for unity but hes kinda mia now and im left to understand and later update the system. The main problem is that im new to OOP programing. So i have a script which controls the system and ive looked all over the system and the other scripts are only being used to give behaviour to the gameobjects called in the scirpts but where are the gameobject stored is what i cant understand. There is a resourses folder and in it are contained three .prefab files but they include only 4,5 gameobjects, where could the others be stored. The interesting thing, though is that the system works fine.But i cant find most GameObjects, they ARENT in the script itself so where could they be stored so i can change,add new ones or interact with the old ones? Many thanks to the whoever explains this to me, i will be really grateful! And thanks for your previous support :slight_smile:

Is the script/scripts on the game object itself? Or is it a script that is kind of stand alone by itself on like an empty gameobject?

Usually what I do is I import a game object into the scene. Then add components to it (like scripts and colliders and rigidbody components). Then you can make that pre-setup game object a prefab and reference the prefab you just made via script.

From there you basically reference the prefab, or if the script is attached to the game object itself you don’t have to use GameObject.Find you simply say something like transform.position = “something here”, and it automatically references the transform of the game object the script is attached to.

GameObject.Find searches through the Hierarchy. So if it were something like this example:

var spider1 : GameObject;
spider1 = GameObject.Find("Spider");  // This will return the game object named Spider in the scene.

“Spider” has to be in your Hierarchy panel in the Unity editor. Which usually means it has to be a game object in the scene. Whether it was placed there before the start of the scene, or whether it gets instantiated or whatnot during the game.

So another thought, is maybe the game objects don’t exist yet in the scene. If, at runtime, he is instantiating game objects through code, then you simply are making copies of a pre-setup game object (prefab) over and over again.

Not sure if any of that helps your problem but thought I’d just throw it out there.

I’ve actually found the gameobjects now they were in a hidden folder lol. Thats why they didnt appear when i was looking. And in the scene they were included but not in the main prefab but in the smallest as children objects and i didnt look for children objects only in that .prefab file ;x Anyways ive acquired enough knowedge to start re-arrenging the system. And ill begin updating it this week. I will also put in the asset store. The system is nothing really interesting though its a smart mist - really realistic, perfect for swamps or even city grounds :slight_smile: Thanks for your help a lot.