Basic scripting concepts

This is the place to find out how to accomplish something specific with EZ GUI.

Basic scripting concepts

Postby Brady » Tue Apr 19, 2011 8:47 pm

This post will cover a few very basic scripting concepts that don't really pertain specifically to EZ GUI, but rather to Unity in general. Everyone using Unity should try to master these in order to be able to accomplish common tasks.

The first thing I will cover here is a very important concept to master, and that is how to access other objects and components from your scripts. Most of the time, to do something with an EZ GUI control via script, you will need to have a variable that points to that EZ GUI component. Note that this is distinct from the GameObject that hosts the component. Having a reference to a GameObject is not the same as having a reference to the component of that GameObject. While you can get the component from the GameObject, that isn't usually necessary as you will see below.

One commonly used technique to "find" the object a script needs to access is to use GameObject.Find(). I generally discourage using this technique as it is slow because it must search the scene for the object it is looking for. This may be acceptable in your start-up code, but definitely not a good idea to do during gameplay. Instead, I recommend using the techniques outlined below. There are two main methods of obtaining a reference to another object or component that we will cover here:

1) Declare a public variable of the desired type, and then drag the object from the scene hierarchy onto the "slot" that appears in the inspector for that script:
Code: Select all
// JS public variable:
var healthBar : UIProgressBar;

// C# public variable:
public UIProgressBar healthBar;


Note that in this example, we are referencing the components directly, not GameObjects. This way, we just get the reference we need and save ourselves the trouble of having to get the component in a second step. If we need the GameObject, this is always readily available using the component's own .gameObject member.

The second method is to attach your script to the same GameObject as the component you're trying to access/manipulate, and use GetComponent() to get a reference to it, like so:
Code: Select all
// JS
var healthBar : UIProgressBar;

function Start()
{
   healthBar = GetComponent("UIProgressBar");

   // Now do something with it:
   healthBar.Value = 1; // 100% health
}

// ---------------------

// C#
UIProgressBar healthBar;

void Start()
{
   healthBar = GetComponent<UIProgressBar>();

   // Now do something with it:
   healthBar.Value = 1f; // 100% health
}


You can also mix these two approaches if you want a central "control" script that needs to access multiple components on your objects. Just declare public variables to all your various GameObjects, and then call GetComponent() on each of them to get the desired components, like so:
Code: Select all
var fireBtnGO : GameObject;
var healthBarGO : GameObject;

function Start()
{
   var fireBtn : UIButton = fireBtnGO.GetComponent("UIButton");
   var healthBar : UIProgressBar = healthBarGO.GetComponent("UIProgressBar");

   var fireBtnMisc : MyMiscComponent = fireBtnGO.GetComponent("MyMiscComponent");
   var healthBarMisc : MyMiscComponent = healthBarGO.GetComponent("MyMiscComponent");
}


Use this approach if there are several components you will need references to on the same GameObject(s), so as to avoid having to drag the same object onto multiple "slots" in the inspector. With this approach, you just drag each GameObject on once, and then your code gets all the components it needs.

If you are getting compiler errors that the "GameObject" or some other type does not have a specific member you are trying to access, you are probably using a reference to the wrong type and need to make sure you are using a reference to the proper component type. For example, GameObject is its own class type and doesn't have a .Value property. Whereas the component type "UIProgressBar" does have a .Value property. So in order to use that .Value property, you must make sure the variable you are using is of that type and has been assigned a reference to the desired component using one of the above techniques.
Brady
 
Posts: 5361
Joined: Tue Jul 06, 2010 11:33 pm

Return to EZ GUI How to...

Who is online

Users browsing this forum: No registered users and 6 guests

cron