Variable used as a method

I have 2 scripts and I’m trying to get one to reference a class in the other:

Script #1 GuiInit.cs

This script generates the error, seems I can’t call Test after creating a new DrawStaticUI Class in it… Any Idea?

I’ve looked around but I can’t find an example that cover just THIS so I can understand the workings of C#…

using UnityEngine;
using System.Collections;

class GuiInit : MonoBehaviour
{
	GuiInit ()
	{
			DrawStaticUI Test = new DrawStaticUI();
			Test();
	}
}

and Script #2 DrawStaticUI.cs

using UnityEngine;
using System.Collections;

public class DrawStaticUI : MonoBehaviour
{
	
	public Texture2D background;
			
	public void OnGUI()
		{
		GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), background, ScaleMode.StretchToFill, true, 10.0f);
		}
	
	void Update()
		{
		OnGUI();
		}
}

Thanks for noob support :slight_smile:

Test is declared as a variable of type DrawStaticUI. It’s not a function so you can’t call Test().

I agree with wolfhunter. if you had a method in DrawStaticUI called

Test2(){ //code here
}

you could use

Test.Test2();

BTW Go back n read a few tutorials… Its been awhile since I’ve used OnGUI but I wasn’t aware you had to call the OnGUI function in update…

Right, OnGUI gets called automatically just like Start and Update and friends.

Right, I added the .OnGUI after Test and now it compiles… I was almost there! Suprises me it wasn’t one of the many things I tried… :face_with_spiral_eyes:

Next stop, it’s asking for an instance of an object, so I create an instance of DrawStaticUI (I think I do anyways) called UI_BKG thru the NewDraw Method and pass arguments thru that (A texture in this instance). It still says I’m not working thru instance of objects… Whats wrong?

Disclaimer!: Everything I do is to learn C#… Sometimes it may look like I’m doing long detours but it simply to compute (In my head) a given concept. Ex: Calling a method from a different script just fell into place with you guy’s comments ;).

Existential Question of the day: Extending a class with “: MonoBehaviour” Generates a warning “Can’t use keyword new with MonoBehaviour” and suggests using AddComponenent… Whats that all about?

Thanks for the help guys!

Updated code:

GuiInit:

using UnityEngine;
using System.Collections;

class GuiInit : MonoBehaviour
{
	
	public Texture2D background;
	
	GuiInit ()
	{
			DrawStaticUI Test = new DrawStaticUI();
			Test.NewDraw(background);
	}
}

DrawStaticUI:

using UnityEngine;
using System.Collections;

public class DrawStaticUI : MonoBehaviour
{
		
	public void Draw(Texture2D background)
		{
		Debug.Log(1000);
		GUI.DrawTexture(new Rect(0,0,Screen.width,Screen.height), background, ScaleMode.StretchToFill, true, 10.0f);
		}
	
	public void NewDraw(Texture2D background)
		{
		DrawStaticUI UI_BKG = new DrawStaticUI();
		UI_BKG.Draw(background);
		}
}

You don’t want to create new MonoBehaviours (unless you do the setup Unity does, and I have no idea what that does).

AddComponent allows you to attach a MonoBehaviour to an existing GameObject (a MonoBehaviour not attached to an object is, I believe, a useless waste of memory which does nothing).