- Home /
Issue selecting gameObject using raycast
I am having issues with selecting units in a turn based strategy game I am building. Below is the script in question. My issues are that for some reason selectedUnit and selectedContainer are not being set when the raycast is being shot out.
void Update () {
if (Input.GetMouseButtonUp (0)) {
Debug.Log ("click");
if (Physics.Raycast (detectUnit, out hitUnit, Mathf.Infinity, 8)) {
//if no unit is selected
if (isSelect == false) {
//set the selectedUnit gameobject equal to the object hit by the raycast
selectedUnit = hitUnit.transform.gameObject;
//set selected container equal to the container script on that object
selectedContainer = selectedUnit.GetComponent<container>();
//if it is player 1's turn
if (currentPlayer == 0) {
//if unit is under player 1 control set selected flag in this script and unit's container script to true
if (selectedContainer.player == 0) {
selectedContainer.isSelected = true;
isSelect = true;
}
//if unit is not under player 1 control, do nothing
if (selectedContainer.player == 1) {
selectedContainer.isSelected = false;
isSelect = false;
}
}
//if it is player 2's turn
if (currentPlayer == 1) {
//if unit is under player 2 control set selected flag in this script and unit's container script to true
if (selectedContainer.player == 1) {
selectedContainer.isSelected = true;
isSelect = true;
}
//if unit is not under player 2 control, do nothing
if (selectedContainer.player == 0) {
}
}
}
//if a unit is selected
if (isSelect == true) {
//if it is player 1's turn
if (currentPlayer == 0) {
//if unit is under player 1 control, deselect old unit and select new unit
if (selectedContainer.player == 0) {
previousSelected = selectedContainer;
selectedUnit = hitUnit.transform.gameObject;
selectedContainer = selectedUnit.GetComponent<container>();
selectedContainer.isSelected = true;
previousSelected.isSelected = false;
}
//if unit is not under player 1 control, do nothing
if (selectedContainer.player == 1) {
}
}
//if it is player 2's turn
if (currentPlayer == 1) {
//if unit is under player 2 control, deselect old unit and select new unit
if (selectedContainer.player == 1) {
previousSelected = selectedContainer;
selectedUnit = hitUnit.transform.gameObject;
selectedContainer = selectedUnit.GetComponent<container>();
selectedContainer.isSelected = true;
previousSelected.isSelected = false;
}
//if unit is not under player 1 control, do nothing
if (selectedContainer.player == 0) {
}
}
}
}
}
}
}
Answer by M-Hanssen · Apr 18, 2016 at 01:09 PM
"Debug" is the magic word here ;-)
Just step through your code with the debugger and you will find your answer
Honestly, I've never used the debugging tool before as I've been able to figure out most of the problems on my own. Is it easy to use?
Read this:
https://unity3d.com/learn/tutorials/modules/beginner/scripting/monodevelops-debugger
In $$anonymous$$onoDevelop, just hit the debug button, set a breakpoint and fire up "Play" in your Unity editor.
I really advice you to use visual studio if you want to debug like a pro ;-)
Used the debug tool. The ray is being cast but nothing else is happening which more than likely means that it is not colliding with anything.
You don't even have to start with the debugger if it's seems complicated, you could ins$$anonymous$$d insert Debug.Log lines here and there to see what parts of your code are reached, and how.
Answer by Chimmly · Apr 18, 2016 at 05:13 PM
Found the issue. The objects I was trying to select did not have mesh colliders attached to them apparently :/
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Raycast2D is inconsistent 0 Answers
2 raycasts on the fpwalker/camera 1 Answer
Why can't I use Selection.GetFiltered with GameObjects? 1 Answer