- Home /
Raycast Having Pointless Errors
private var enter : boolean;
var myAudioClip: AudioClip[];
var mySkin : GUISkin;
function Update ()
{
if(Input.GetMouseButtonDown(0) && enter)
{
Ray = camera.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast (ray, out hit3, 5.0f))
{
if(audio.mute)
audio.mute = false;
else
audio.mute = true;
}
}
}
function OnGUI()
{
GUI.skin = mySkin;
if(enter)
{
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 200, 200), "do whatever");
}
}
This is getting errors with just line 10, saying that the Unexpected token: 5.0f. and
BCE0044: expecting ), found ',' and finally BCE0044: expecting ), found 'hit3'
What is the issue, those shouldn't be errors, they should be there? Why is it considering them errors?
that fixes the issues, but now it says that BCE0049: Expression 'Ray' cannot be assigned to. BCE0005: $$anonymous$$ identifier: 'ray'. and BCE0005: $$anonymous$$ identifier: 'hit3'. ???
Answer by Nition · Mar 20, 2014 at 02:29 AM
Your issues were:
"out" isn't needed in UnityScript
"f" on floats isn't needed in UnityScript
"ray" was never defined, only "Ray", which was the type you needed but not a variable in itself
"hit3" was never defined
Make sure you carefully read error messages, as they'll usually give you a good idea of what's wrong. If it says "Unknown identifier: 'ray'.", then ray mustn't have been defined somewhere. You can double-click the messages to go straight to where the error occurred.
Fixed version:
function Update ()
{
if(Input.GetMouseButtonDown(0) && enter)
{
var ray : Ray = camera.ScreenPointToRay(Input.mousePosition);
var hit3 : RaycastHit;
if (Physics.Raycast(ray, hit3, 5.0))
{
if(audio.mute)
audio.mute = false;
else
audio.mute = true;
}
}
}
well, now im getting no errors, but it does not work? maybe im using the wrong colliders? its a box collider and it dosnt work if its a trigger or not
You can use Debug.DrawRay to check which direction the ray is really going.
$$anonymous$$aybe the box is more than 5m away? You could check the ray's direction AND length with something like:
Debug.DrawLine(ray.origin, 5.0 * ray.direction, Color.green, 10.0f);
didnt show up, maybe im putting the script on the wrong object?
It shouldn't matter which object it's attached to really. Chuck a print("something"); line inside your If statement and make sure it's actually being called.
I think you also need the Gizmos button turned on to see Debug stuff.
Your answer
Follow this Question
Related Questions
Raycast hits and affects all items within the same tag 2 Answers
Raycast doesn't update 1 Answer
Unity 3.4.0 Running really slow 1 Answer
Raycasting in script suddenly stopped working 1 Answer
GameObject[] Issues 1 Answer