x


mouse over enables another script

I am trying to make a script enable on mouse over, so that a specific menu will work only if the mouse is on that specific object. When the mouse is not on the object the same button does a different action. Having trouble so I thought I'd come to the community for help

Here is the code so far: var rightclickworking : int = 0;

function OnMouseOver () { rightclickworking = 1; }

function OnMouseExit () { rightclickguiworking = 0; }

function Update () { var script1 = GetComponent("RightClick");

if (rightclickworking == 1) {
script1.enabled = true;
}
else if (rightclickworking == 0) {
script1.enabled = false;    // <---- here is my issue
}

}

The error I am getting is: "Object reference not set to an instant of object" at line 15 and 18 where I specified.

This script is attached to the object and script1 is attached to the camera. Could anybody help me with what I am doing wrong?

more ▼

asked Nov 05 '11 at 08:49 AM

garanon gravatar image

garanon
31 8 10 12

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Directly calling GetComponent(RightClick) only allows you to access components on the same gameobject.

If you need to access to a component on another object, you must first have a reference to that gameobject and then use otherGameObject.GetComponent(RightClick).
Ideally you get that reference only once if the gameobject doesn't change, e.g.:

var camera : GameObject; // assign in inspector
var script1 : RightClick;

function Awake() {
    script1 = camera.GetComponent(RightClick);
}

Sidenote: Just use booleans for flags instead of int:

var rightclickworking : boolean = false;
function OnMouseOver () { rightclickworking = true; }
function OnMouseExit () { rightclickguiworking = false; }

Now you can just do:

if (rightclickworking) {
    // ...
} else {
    // ...
}

... or just:

script1.enabled = rightclickworking;
more ▼

answered Nov 05 '11 at 09:28 AM

gfr gravatar image

gfr
451 1 3 4

Thanks for the help :)

Nov 05 '11 at 10:07 AM garanon

You're welcome. If your problems are solved consider upvoting and/or accepting answers on your questions.

Nov 05 '11 at 10:18 AM gfr

I have upvoted as I understand what you're saying but no matter how many times I go over your code I can't make sense of what I must do to get it working for myself. I don't understand why you use "var script1 : RightClick" which is affecting the rest of the code

I will get some sleep and hopefully make sense of it tomorrow but I've literally been reading it over and over for an hour trying my own variations with no result

Nov 05 '11 at 01:47 PM garanon

var script1 : RightClick; is declaring the variable so you have it available in that script. When the scene is running, it's set to the script component from your camera object in Awake(). If i understand your setup right it should be enough to drag'n'drop the camera object in your scene on that slot in the inspector or to initialize it in Awake() as well using e.g. Camera.main.

Nov 05 '11 at 02:23 PM gfr

Well thanks for the help but I'm still confused :P due to me being a newbie I assume, my tutor just threw me into the deep end so I have little knowledge of the things I am or aren't achieving. I'll mark it as right though in case anybody else wants to have a look but I guess not for me :P

Nov 05 '11 at 03:14 PM garanon
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x143
x38

asked: Nov 05 '11 at 08:49 AM

Seen: 718 times

Last Updated: Nov 05 '11 at 03:21 PM