- Home /
problem with using classes and coroutines
i just figured that i can't use yield in a class that is derived from the object like this:
class CLabels extends Object{
var startPoint : Vector3; var endPoint : Vector3;
var labelRect : Rect;
var rPosx : int; var rPosy : int;
var rWidth : int; var rHeight : int;
var target : Transform;
var cam : MainCamera; var lineMaterial : Material;
function DrawLabel(currentTarget : Transform){
Start();
CalculateLinePositions();
OnGUI();
OnPostRender();
MonitorRectState();
} //END OF DRAW FUNCTION
//OTHER FUNCTIONS HERE
}
basically what i was trying is to make a class that will draw label on top of selected object. it draws line from the object to the rectangle that holds information about the object. everything works fine, i should say worked fine, until i wanted to make the function that will slide the rectangle to the side when it is located on top of the object, so it doesnt obscure it. so what now? how to use coroutines?
also i am new in writing classes, i dont know if i am doing it the right way. i dont have a constructor, i dont know how to use it. also i am calling OnGUI and OnPostRender from within functions that i wrote. i dont know is that the way to do it...
thanks!
also i noticed that when i want to use image button inside the class like this:
var image : Texture2D ...
if(GUI.Button(Rect(200,200,40,40),image)){
//do something;
}
it doesnt work, if i do the same thing in the usual script that is attached to the camera i get the button image... go figure..i know i am doing something wrong but i dont know what exactly...
Answer by Eagle32 · Aug 23, 2010 at 09:25 PM
also i am calling OnGUI and OnPostRender from within functions that i wrote.
Don't.
it doesnt work, if i do the same thing in the usual script that is attached to the camera i get the button image... go figure..i know i am doing something wrong but i dont know what exactly...
That's not going to work, a class has to derive from MonoBehaviour if you want to be able to use OnGUI, Update, Start, Awake, etc
EDIT
I just realised you're writing javascript not C#. The class decleration isn't needed in javascript. Take that out and the script will automatically extend from MonoBehaviour.
OnGUI, Update, Start, Awake, etc all get called by Unity automatically at the appropriate times. OnGui is where all your drawing code should be.
What does MonitorRect state do? I don't know so I'll guess update is the best place for it.
var startPoint : Vector3; var endPoint : Vector3;
var labelRect : Rect;
var rPosx : int; var rPosy : int;
var rWidth : int; var rHeight : int;
var target : Transform;
var cam : MainCamera; var lineMaterial : Material;
function Update(){
CalculateLinePositions();
MonitorRectState();
}
//OTHER FUNCTIONS HERE
ok, i figured that i shouldnt do it that way...but how to do it then??
I've update the answer a bit, but you need to provide more information about what you are trying to do with yield and coroutines for anyone to be able to help more.
Your answer
Follow this Question
Related Questions
C# yield not doing anything 2 Answers
How to use yield within a class function 2 Answers
Null Reference Exception after updating from 4.0 to 4.1.5 (coroutines) 0 Answers
Last item not being moved (yield problem) 0 Answers
Coroutoutine doesnt work properly. 3 Answers