OnGUI with option from other function

#pragma strict

var displaymessage = false;

function GiveWood (WoodGet : int) 
{
	displaymessage = true;
}

function OnGUI ()
{
	if(displaymessage)
	{
		GUI.Label(Rect(25, 25, 100, 30), "Wood x" + WoodGet);
	}
}

I want do this, but i have error about this

GUI.Label(Rect(25, 25, 100, 30), "Wood x" + WoodGet);

How do it properly ?

WoodGet has to be a GUIContent.

GUI.Label(Rect(25, 25, 100, 30), GUIContent("Wood x" + WoodGet)); ?

btw WoodGet var is in other script ;/ i cant move it

Then you should use GetComponent to reference “WoodGet” from the other script. Here’s a simple way of doing it :

var yourGameObject  : Transform; // The GameObject that has the Script.

function OnGUI(){
var woodGet = yourGameObject.GetComponent("NameOfTheScript").WoodGet;
#pragma strict

var displaymessage = false;
var getscript : Transform;

function GiveWood (WoodGet : int) 
{
	displaymessage = true;
}

function OnGUI ()
{
	if(displaymessage)
	{
		var woodGet = getscript.GetComponent("WeaponSystem").WoodGet;
		GUI.Label(Rect(25, 25, 100, 30), GUIContent("Wood x" + woodGet));
	}
}

I have in my WeaponSystem.js this

if(Input.GetButtonDown("Fire1"))
	{
		var hit : RaycastHit;
		if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
		{
			Distance = hit.distance;
			if(Distance < MaxDist)
			{
				var WoodGet = Random.Range(minWood, maxWood);
				hit.transform.SendMessage("GiveWood", WoodGet, SendMessageOptions.DontRequireReceiver);
				hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
			}
		}
	}

and after i put getscript : Transform; i cant choose my object in Inspector tab i dont know why ;<

and Cube have this script (GiveWood.js form 1st post)

My bad I meant without the quotes :

 var woodGet = getscript.GetComponent(WeaponSystem).WoodGet;

Okay :slight_smile:
But now i have this error:

#pragma strict

var displaymessage = false;
var getscript : Transform;

function GiveWood (WoodGet : int) 
{
    displaymessage = true;
}

function OnGUI ()
{
    if(displaymessage)
    {
        var woodGet = getscript.GetComponent(WeaponSystem).WoodGet;
    	GUI.Label(Rect(25, 25, 100, 30), GUIContent("Wood x" + woodGet));
    }
}

And i must change in WeaponSystem.js this:

function Update ()
{
if(Input.GetButtonDown("Fire1"))

    {

        var hit : RaycastHit;

        if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))

        {

            Distance = hit.distance;

            if(Distance < MaxDist)

            {

                var WoodGet = Random.Range(minWood, maxWood);

                hit.transform.SendMessage("GiveWood", WoodGet, SendMessageOptions.DontRequireReceiver);

                hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);

            }

        }

    }
}

to this

 var WoodGet;
function Update ()
{
if(Input.GetButtonDown("Fire1"))

    {

        var hit : RaycastHit;

        if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))

        {

            Distance = hit.distance;

            if(Distance < MaxDist)

            {

                WoodGet = Random.Range(minWood, maxWood);

                hit.transform.SendMessage("GiveWood", WoodGet, SendMessageOptions.DontRequireReceiver);

                hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);

            }

        }

    }
}

and i dont know it is good or not ;p

Have you assigned getScript in the inspector?

Yes this GiveWood.js is in Cube and i assigned this Cube to the getscript in inspector tab, look:
http://imageshack.com/a/img35/5207/of20.png
If u dont know what’s wrong i can give u full WeaponSwitch and Givewood scripts, maybe this help.

Yeah post the scripts.

WeaponSystem.js

#pragma strict

//var msg = false;
var Distance : float;
var MaxDist : float = 1.5;
var minWood : int = 1;
var maxWood : int = 2;
var Damage : int = 10;
var Axe : Transform;
var Knife : Transform;

var WeaponAxe : GameObject;
var WeaponKnife : GameObject;

var WoodGet;

function Update ()
{
	if(Input.GetKeyDown(KeyCode.E))
	{
		SwapWeapons();
	}
	if(Input.GetButtonDown("Fire1"))
	{
		//Attack animation
		if(WeaponAxe.active == true)
		{
			Axe.animation.Play("HitAxe");
		}
		if(WeaponKnife.active == true)
		{
			Knife.animation.Play("HitKnife");
		}
		//End
		//Attack function / Collecting function
		var hit : RaycastHit;
		if(Physics.Raycast (transform.position, transform.TransformDirection(Vector3.forward), hit))
		{
			Distance = hit.distance;
			if(Distance < MaxDist)
			{
				//msg = true;
				WoodGet = Random.Range(minWood, maxWood);
				hit.transform.SendMessage("GiveWood", WoodGet, SendMessageOptions.DontRequireReceiver);
				hit.transform.SendMessage("ApplyDamage", Damage, SendMessageOptions.DontRequireReceiver);
			}
		}
		//End
	}
	if(Axe.animation.isPlaying == false)
	{
		Axe.animation.CrossFade("IdleAxe");
	}
	if(Knife.animation.isPlaying == false)
	{
		Knife.animation.CrossFade("IdleKnife");
	}
}

function SwapWeapons ()
{
	if(WeaponAxe.active == true)
	{
		WeaponAxe.SetActive(false);
		WeaponKnife.SetActive(true);
	}
	else
	{
		WeaponAxe.SetActive(true);
		WeaponKnife.SetActive(false);
	}
}

/*function OnGUI () {
	
	if(msg == true) {
		
	}
	
}*/

GiveWood.js

#pragma strict

var displaymessage = false;
var getscript : Transform;

function GiveWood (WoodGet : int) 
{
    displaymessage = true;
}

function OnGUI ()
{
    if(displaymessage)
    {
        var woodGet = getscript.GetComponent(WeaponSystem).WoodGet;
    	GUI.Label(Rect(25, 25, 100, 30), GUIContent("Wood x" + woodGet));
    }
}

Hey, I just tried it out and i get no errors.

I took a closer look at your pic and noticed that you’re assigning a gameObject with the GiveWood.js attached to it

You need to assign a gameObject with the WeaponSystem.js attached to it.

I don’t get it
gameObject, u mean WoodGiver (Cube) ?
I need this GiveWood.js to be attached with WoodGiver, because it check my hit (if i hit it then display message).
Or maybe i must put this var getscript : Transform;
to WeaponSystem and attach WoodGiver to it ?

Sorry, but i don’t get it, im new ;D

My WeaponSystem need to be attached with “Meele” (u can see it on my screenshot)

Basically, the variable getScript needs to reference the WeaponSystem script. So you’ll have to assign it with a gameObject( any object in your scene ) that has the WeaponSystem.js attached to it.

If “Meele” has the WeaponSystem.js attached to it, then assign “Meele” as the getScript variable in the inspector.

U are my master ! My GOD . Thank you so much :smile: :****

I have another problem but not with this it just show in console and i dont know what is this

NullReferenceException: Object reference not set to an instance of an object
UnityEditor.Graphs.AnimationStateMachine.TransitionInspector.OnEnable () (at C:/BuildAgent/work/d3d49558e4d408f4/Editor/Graphs/UnityEditor.Graphs/AnimationStateMachine/TransitionInspector.cs:71)

And if i want place there some graphic not text what i must do ?

haha…

The 1st error is letting you know that an object you’ve assigned isn’t being referenced correctly.
I’m not quite sure what that 2nd error is - Try re-staring Unity and maybe it’ll go away.

If you want Graphics then you can use GUI.DrawTexture to display an image on the screen…

var graphic : Texture;

function OnGUI(){

GUI.DrawTexture(Rect(Screen.width / 2 - 50, Screen.height / 2 - 50 , 100 , 100),graphic);

}

XDD oh i saw it but i didnt know what is this ;D

Okay, thanks :wink: And can i do some delay between this GUI drawtexture or label ? Now it’s like x999 on sec (thies message)xD

U know, english isnt my language and sometimes its hard (for me) to understand some texts ;p

Ok.

Well, I’m not so sure how to do the delays but it’s definitely possible :slight_smile:

Okay, so thanks for help :wink: