Simple Questions - Object Variables (and others)

  1. I am looking for a way to use a variable across multiple scripts. I know it can be done but do not know the syntax in Unity. This should be pretty easy since I just want the object to have a variable such as health or power that multiple scripts may access and use.

  2. I am trying to add an object that will restore health when you run into it. I tried something along the lines of this:

//This is not the actual code since I deleted it when it didn't work. But this was the general idea.
var boost : GameObject;
function OnCollisionEnter(hit : collision) {
 if (hit == boost)
  power += 2; //Or some other number
}
  1. Finally is their a GUI script that allows you to draw a camera border? I have a rearview camera located in the lower left hand corner of the screen and sometimes it will blend with the primary front-facing camera. How do you draw this border? When I tried drawing a rectangle it drew it above the camera and looked terrible.

In my coding I am using Java since I understand it more then C.
In advance thanks for the help.

  1. http://unity3d.com/support/documentation/ScriptReference/index.Member_Variables_26_Global_Variables.html

As you can see, when you create a variable (var), you can put private ou static before it. With a static var healthValue in a HealthManager.js file for exemple, in any script, you can access it by writing HealthManager.healthValue.

Without static, the script HealthManager must be put on a gameObject (named “HMO” for exemple). To access the value : GameObject.Find (“HMO”).GetComponent (HealthManager).healthValue.

(HealthManager without " ").

If I’m not mistaken, the static var is the same for every copy of a component, so static var healthValue from gameObjectOne is the same as static var healthValue from gameObjectTwo. It should make sense, we don’t need to specify an object to access it.

  1. If a boost can be recognized by a tag, then
function OnCollisionEnter(hit : Collision) {
 if (hit.gameObject.tag == "boost")
  power += 2; //Or some other number
}

Don’t forget to destroy the gameObject or anything else, to make sure the bonus is usable only once.

  1. I haven’t understood. Explain again please.

Thanks for the help on 1 and 2.

In number three I am drawing two camera’s. I want to draw a border, just a black line, around the camera screen since sometimes it can get confusing. How do I do it?

Think of a multiplayer split screen game. How would you split the camera’s. The only difference with this and my problem is that my camera’s overlap.

I don’t have enough knowledge so I can’t provide you the best answer.

But a lesser solution would be to make a black GUITexture under the camera, slightly bigger than the rect of the camera. It seems that the position value Z of the transform component is the depth (0 is the bottom of the stack/rendered first, and hidden by anything of superior depth).

You could create a GUI Texture in the editor and give it a black line and just scale it up, and add it for all 4 lines (top bottom left right) or draw a big one with apha channel and apply it to the screen.

You can use the same with the OnGUI function and call GUI.DrawTexture, but this will be a bit slower i think but gives the same effect.

The DrawTexture doesn’t have a depth input, do it ? It wouldn’t work I think.

Why do you need depth if you want to make a black border around your camera screen? You simply use the Rect to adjust the position and size of that line.

I reread, I just understood. Well, why not.

That makes sense. Thanks for the help.