Trying to access a variable from another script attached to same object.

oh boy… let me start by saying I have only been using Unity3d for about 2 weeks now and that my C# skills are not so… well… sharp. I am working on a school project.

I am having a hell of a time references a variable in one script with another script. The 2 scripts are even on the same object so it blows my mind that the program has such a hard time handling this. I KNOW that there are already tons of posts on this subject, I have poured over a lot of them and tried to use the code from the answers but something just isn’t working right or I’m not interpreting something right. I was hoping for a specific example using my variables,script-names and code.

I am using Unity 3.5.5 and programming in C#.
My first script is EnemyHealth.cs and contains the following
using UnityEngine;
using System.Collections;

public class EnemyHealth : MonoBehaviour {

public int maxHealth = 10;
public int currentHealth = 10;

public float healthBarLength;


// Use this for initialization
void Start () {
	healthBarLength = Screen.width / 2;
	
}

There is more there but it seems irrelevant as it is mostly GUI stuff. The variable I want to use in script 2 is the currentHealth stat. I have named script 2 EnemyDeath.cs, it is attached to the same game object and want to get it to pull the current health and use an if statement to detect if the current health <= 0 then “whatever” happens… (it turns red, it falls over)… but whatever I try I can not get the variable to pass along the info. Someone please give me something in a format where I could just cut and paste it into my code as I am just not savvy enough to transmute any “examples”.

Also - please don’t just paste in “http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html” as the answer; as that is incredibly unhelpful(to someone of my skill-level) and when I have enough karma I will go around and thumbs-down those posts into oblivion.

Here are some links I strongly suggest to all new users :

Start at the bottom and work up : Unity 3D Student - 3d Modelling

Start at the bottom and work up : Unity 3D Student - 3d Modelling

this is the YouTube link for the above as one playlist : Unity Tutorials - Essentials 00 - Projects - Unity3DStudent.com - YouTube


That is good to get started. Then start with a small tutorial, this is a simple 2D space shooter : Index of /ProofOfConcept_1 : click on the videos part1.mp4 part2,3,4 =]

I found another by Eric : http://wiki.unity3d.com/index.php?title=2DShooter : http://forum.unity3d.com/threads/7883-2D-shooter-tutorial

By then you should be getting the hang of things and starting to have ideas of your own. When you decide what kind of game you want to make, then look at each part you’ll need. For example, if you want to make some terrain then walk around it with a character : CG Cookie | Learn Blender, Online Tutorials and Help - CG Cookie | Learn Blender, Online Tutorials and Feedback

Basically then just search for tutorials, there are many out there, either written or on youtube.

the Unity Wiki tutorials : http://wiki.unity3d.com/index.php/Tutorials

A big list of tutorials : How can I start learning Unity fast? ( List Of Tutorials ) - Unity Answers

A very helpful 'site, all in C# : http://unitygems.com/

Helpful page with information on using Built-In Arrays and Lists (you’ll need this later!) : unifycommunity.com?

The unity wiki link above is very handy with lots of scripts and shaders too (just check out all the links down the left, and the tabs along the top : http://wiki.unity3d.com/index.php/Scripts )

http://forum.unity3d.com/threads/132628-How-to-help-the-Absolute-Beginner

If you know what kind of game you want to make, edit your question and write there what you are thinking of, then there may be a tutorial out there for that, Happy Coding =]