Hello forum
Before i start i want to say that im sorry for my english and my lack in scripting.Im new to game design.
I am having some problems with my cursor textures and mouseOver mouseExit commands.First of all im using 2 different textures for the mouse, one arrow and one hand. The thing i want is on mouseOver the arrow changes to hand and vice versa.
Now the problem happens when i use the same script for a second object. As i move the cursor on top of the objects the arrow remains on top of the hand texture but when the cursor exits the object it changes back to arrow.
This is the script i use.
var cursor : Texture2D;
var link : Texture2D;
var normal : Texture2D;
function Update (){
Screen.showCursor = false;
}
function OnMouseOver () {
Screen.showCursor = false;
cursor = link;
}
function OnMouseExit () {
Screen.showCursor = false;
cursor = normal;
}
function OnGUI () {
GUI.Label(Rect(Input.mousePosition.x-12, Screen.height - Input.mousePosition.y-10, 100, 100), cursor);
}
Thank you in advance
Your solution revolves around two objects calling the same OnGUI. One says make it a arrow, the other says make it a hand.
Your best result comes from simply making a CursorController (object and script) So you make an object, apply the script then tell it what the current cursor is. It then would not let you draw the GUI twice for the cursor.
have a gander at my solution. Probably a bit overkill… The biggest part is that you dont want two objects contradicting your script. So some logic is applied to not let that happen.
//CursorController.js
// should be placed on an object in the base scene level called CursorController
enum CursorType{
normal,
link,
hand
}
var defaultCursorState : CursorType=CursorType.normal;
var currentCursorState : CursorType=defaultCursorState;
var controllingObject : GameObject;
var normal : Texture2D;
var link : Texture2D;
var hand : Texture2D;
function Update(){
}
function OnGUI () {
var cursor : Texture2D=var normal;
if(currentCursorState == CursorType.link link) cursor=link;
if(currentCursorState == CursorType.hand hand) cursor=hand;
GUI.Label(Rect(Input.mousePosition.x-12, Screen.height - Input.mousePosition.y-10, 100, 100), cursor);
}
function SetCursor(newState : CursorType, fromObject : GameObject){
if(newState <> defaultCursorState){
controllingObject = fromObject;
currentCursorState=newState;
} else {
if(controllingObject == fromObject)
currentCursorState=newState;
}
}
//ObjectCursor.js
function OnMouseOver () {
try{
var CC = GameObject.Find("CursorController").GetComponent("CursorController");
CC.SetCursor(CursorType.link, this);
}catch(e){}
}
function OnMouseExit () {
try{
var CC = GameObject.Find("CursorController").GetComponent("CursorController");
CC.SetCursor(CC.defaultCursorState, this);
}catch(e){}
}
First thank you for your response
I created an object and attached the first script to it and i attached the second script on the object that i want to interact with.The result was that the cursor doesnt change when i move it over the object that i want it to interact with.
On the CursorController script i have set the controlling object as the object the script is attached to. Am i doing something wrong?
Thank you for your time
And that is what I get for not testing things… lol
OK, Evidently in order for “this”… to be passed, it required “this.gameObject” I also checked out the code and made it all work.
//CursorController.js
// should be placed on an object in the base scene level called CursorController
enum CursorType{
normal,
link,
hand
}
var defaultCursorState : CursorType=CursorType.normal;
private var currentCursorState : CursorType=defaultCursorState;
private var controllingObject : GameObject;
var normal : Texture2D;
var link : Texture2D;
var hand : Texture2D;
function Start(){
Screen.showCursor = false;
}
function Update(){
}
function OnGUI () {
var cursor : Texture2D=normal;
if(currentCursorState == CursorType.link link) cursor=link;
if(currentCursorState == CursorType.hand hand) cursor=hand;
GUI.Label(Rect(Input.mousePosition.x-12, Screen.height - Input.mousePosition.y-10, 100, 100), cursor);
}
function SetCursor(newState : CursorType, fromObject : GameObject){
if(newState != defaultCursorState){
controllingObject = fromObject;
currentCursorState=newState;
} else {
if(controllingObject == fromObject)
currentCursorState=newState;
}
}
//ObjectCursor.js
function OnMouseEnter() {
//try{
var CC = GameObject.Find("CursorController").GetComponent("CursorController");
CC.SetCursor(CursorType.link, this.gameObject);
print(CC.currentCursorState);
//}catch(e){}
}
function OnMouseExit () {
//try{
var CC = GameObject.Find("CursorController").GetComponent("CursorController");
CC.SetCursor(CC.defaultCursorState, this.gameObject);
//}catch(e){}
}
NOTE: I commented the try tag. so it displayed the error after that… 
I also attached the .js files in a zip file, along with some pointers. (They have to be centered in the image file to point correctly)
544769–19214–$mouseController.zip (26.6 KB)
Thanks for this. In some ways this works better than what I was doing before.
However, I still encounter a problem that I’m hoping you could help me with (I have asked elsewhere with no luck).
I am using the change-cursor script in conjunction with a script that opens a web page when you click on the object. Everything works fine with Internet Explorer, but with Firefox, once the pop-up window opens, the cursor vanishes. Any idea what’s going on there?