RayCast/Scripting Question.

Hello guys,

I have ran headlong into another problem in my quest to get a basic FPS thingy working. This time it’s getting the cross-hair to change colour.

I should mention that I do have a function that will change the cross-hair colour if I select it using a public var in the Inspector, or, even if I set it by shooting a specific object with an OnCollisionEvent. The problem stems from trying to get the cross-hair graphic to change using a RayCast and/or C#.

The code is I am using is from an older book like this it’s all I know, there is probably a better way - in fact , I’m sure there has to be:

Declaring the Var:
   internal var cChange: GameObject;
In Start:
   cCHange=GameObject.Find("CursorHolder");
   //there is a script there residing there called HideCursor.js
   //and a function there called cursorColourChange
Used In function somewhere:
cChange.SendMessage("cursorColourChange",true);

The above in JavaScript works “after” shooting an object: But when I plugged these into the RayCast script it doesn’t.

Declaring:
   internal GameObject cChange; //?? is this correct
In Start:
   cChange = GameObject.Find ("cursorHolder");
In Function:
   cChange.SendMessage("cursorColourChange",true);

And when the raycast hits the specific object, nothing occurs. So I need to know firstly if these should work in the C# script particularly the declaration, or, is there something different going on with the RayCast Script.

Code except:

   if (Physics.Raycast (this.transform.position, this.transform.forward, out hit, RayLength)){
     //test to see if hitting an object with ray
     //without pressing fire will work - it does
     if (hit.transform.tag == "enemy") {
       print ("I touched: enemy");
       //Destroy (hit.transform.gameObject);
       cChange.SendMessage ("cursorColourChange", true);
       }
     ......

To anyone who can help here is a “huge” thankyou in advance.

Regards,
CK.

Is there a reason why you can’t stick to JS to do the raycasting? Mixing languages will always result in a bit of a mess down the road.

In the JS script, you use “CursorHolder” as the game object name, but in C#, you’re using “cursorHolder”. However, since you’re not getting a null reference exception, I reckon you must’ve changed the name of the game object when writing the C# script and didn’t update the JS script (or you just typo’ed). Either that, or you also have a “cursorHolder” game object that doesn’t have the HideCursor script - though that seems highly unlikely.

There’s nothing wrong with your declarations, though it is a bit unusual to use the “internal” keyword in Unity; normally you’d use “private” or “public” there, in this case “private”. Internal is public but only for all files within the same assembly, but in your Unity project you’re always working within one assembly, so you might as well use “public”.

I can’t see a reason why it doesn’t work with the provided code. Can you also post HideCursor.js?

Well, the only RayCasting code I have found used C#, so, I thought that possibly Raycasting had to be done in C#. I thought that because it might be more efficient (faster) in it’s usage. It’s has also become the ‘de facto’ standard.

As you said mixing language was a bit of a mess for me so I rewrote the functions using JavaScript and a healthy dose of the unity docs.

That mistake you found was a typo - I was trying to edit the message whilst editing and trying to get it to match and it was a real pain. I ended up typing them in by hand, not a particularly bright thing to do :sweat_smile:.

With all the code in the same scripting language, I was able to get the project straighter in my head, and it did work … in the end.

Thanks for your advice :sunglasses:.

1 Like