OnGUI messes with update(). How do i solve it?

Lo there… Ima Unity noob in need of help.

In my update() i have some code wich enables me to drag and drop items across the scene using RayCastHit.
I recently added a OnGUI() function and suddenly i got alot of problems with my draging and dropping

Even if my OnGUI funciton is empty, i get these problems. I tried to google this problem and suspects it may have something to do with the event handeling, but is there a way around this?

Also, a separate question. If i create a c# class in Unity IDE and name it “test”. How do i instansiate it from another class?

test asd = new test();

doesnt seem to do the trick… I would like to make a istance of the class so i can call funcitons in it.

Hope you guys can help a noob out. Thanks.

You didn’t say what the problem is.

That looks right. Is it not working? (It’s common convention to capitalize class names, but what you have there should work.)

Didnt describe the problem enough. Sorry =)

The problem is that if i have a OnGUI() function in the class, the object that i am draging starts jumping funny. The code in the update() looks like this:

if (Input.GetMouseButtonDown(0))
        {
            activeCard = null;
            RaycastHit hit = new RaycastHit();
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast (ray, out hit))
            {
                activeCard = hit.collider;
                mouseOffset = new Vector3(activeCard.transform.position.x, activeCard.transform.position.y, activeCard.transform.position.z) - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, CAMERA_HEIGHT));
            }
        }
        else if (Input.GetMouseButton(0))
        {   
            if (activeCard != null)
            {
                Vector3 mousePos = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, CAMERA_HEIGHT - Mathf.Min(activeCard.transform.position.y + 0.2f, 1)));
                activeCard.transform.position = mousePos + mouseOffset;
            }
        }
        else if (Input.GetMouseButtonUp(0))
        {
            activeCard = null;
        }

When i start draging the object, i draw it closer to the camera so it looks like im picking it up. But when i have OnGUI() in my class, it constantly jumps up and down (closer and further fromt he camera) as im draging the object.

Your test() problem is probably to do with namespace.

If test() isn’t in the same script, it would need to be scriptname.test() or add “using scriptname”

Ok…So after doing some digging, i found that my problem was NOT in the update() function at all.

The Objects that are dragable have a OnMouseEnter() funciton that tweens their scale value so that they become bigger when you hover them. It also has a OnMouseExit() that tweens the size down again.

When i add the OnGUI() function, these two functions gets called repeatedly as if the mouse enters and exits them constantly… Why?

It’s a bug.

–Eric