- Home /
OnMouseDown, Raycast, Error
Hello here i have a part of a script that reads, if i press on the gameobject then a boolean will be activated and an object will be activated, but if i click outside of the gameobject then the boolean will be set to false, and the object will be set to false, but i get this error when i click on the gameobject:
NullReferenceException: Object reference not set to an instance of an object worker_Controller.OnMouseDown () (at Assets/Scripts/worker_Controller.js:21)
Any ideas? Thanks :) here is my code:
function OnMouseDown ()
{
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray) && hit.transform.gameObject == "Worker")
{
Selected = true;
SelectedRing.SetActive(true);
}
else
{
Selected = false;
SelectedRing.SetActive(false);
}
}
Is SelectedRing predefined? Guessing here, but do you have it public and need to assign the gameobject in the inspector?
Answer by TrainWrek · Jun 24, 2015 at 12:05 AM
Your hit variable is never assigned to so it will always remain null. You should use "Physics.Raycast(ray, out hit);" instead.
function OnMouseDown () {
var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, out hit) && hit.transform.gameObject == "Worker") {
Selected = true;
SelectedRing.SetActive(true);
}
else {
Selected = false;
SelectedRing.SetActive(false);
}
}
I get a lot of errors in row 21, 25, & 31. It says that the code is missing some symbols
Answer by Ibzy · Jun 23, 2015 at 06:53 PM
This is hard to troubleshoot without knowing the full setup of your scene: is the above script attached to the object you want to select? Theoretically you won't need to fire a Raycast as the OnMouseDown has already detected a click on the object in question.
Also, your check of hit.transform.gameObject == "Worker"
will return false. Perhaps you mean hit.transform.gameObject.name == "Worker"
or hit.transform.gameObject.tag == "Worker"
?
Well the script that is shown above is attatched to the "Worker" and the gameObject is named "Worker" the gameobject has also an boxcollider, and i can click on the worker, but when i do so the error apears
Here is a picture of my scene:
Looks to me like you don't need the Raycast for a start - On$$anonymous$$ouseDown will indicate you have clicked on him. I assume what you are trying to achieve is to highlight the worker you have selected with a blue ring, and then if you click not on him the ring disappears?
yes thats what i want, but i still need some kind of code to do that
Ok, cool. So I understand what you're trying to do.
2 options: 1. try what you have but remove the raycast, the game already knows you clicked on that worker as it's his script that's being activated. 2. Have a gamemaster object with a script that checks for mouse clicks on Update() and does the raycast there. from here you can have it activate the ring of the selected object IF it is a worker, otherwise clear selection.
Answer by adriandevera · Jun 23, 2015 at 03:58 PM
You simply need to have a collider on that object. Without one theres nothing for the raycast to collide with and check in with the bool if it did.
You will need to add it in the inspector or you can do it through code if your doing it on runtime.
SelectedRing.AddComponent();
You can use other colliders as well such as MeshCollider, SphereCollider etc.
I have a collider on the object that this script is attatched to
Your answer
Follow this Question
Related Questions
Mouse Click + Raycast + Colliders 2 Answers
Realistic Bow and Arrow Physics? 2 Answers
GraphicRaycaster not detecting UI elements 0 Answers
Raycast from a 3rdp controller 1 Answer
Raycast on ui elements not working? 0 Answers