- Home /
Why does this Unit-Selection Code not work
Hello! I want to add Unit-Selection, but the following code does not work. There is no drawn Rectangle although I implemented that;
private void SelectingObject()
{
if (Input.GetButtonDown ("Fire1"))
{
mousePos = Input.mousePosition;
}
else if (Input.GetButtonUp ("Fire1"))
{
float[] difference = new float[2];
difference[0] = Input.mousePosition.x - mousePos.x;
difference[1] = Input.mousePosition.y - mousePos.y;
Rect selectedRect = new Rect(mousePos.x, mousePos.y, difference[0], difference[1]);
GUI.color = Color.green;
GUI.DrawTexture (selectedRect, rectTexture, ScaleMode.ScaleToFit);
}
}
I did not implement things like "only select selectable units", "erase the rectangle" etc... because I want that the User can draw a rectangle. I already read that I've to call SelectingObject() in a event called "OnGUI". But if I do this, how do I fire OnGUI?
Hoping for Help...
Is this a 2d ( straight top-down) game? If not, your whole approach is wrong.
Answer by robertbu · Feb 22, 2014 at 09:13 PM
If OnGUI() exists in your code, then Unity will call it. So you add to your script above:
void OnGUI() {
SelectObject();
}
There is another issue here. OnGUI() gets called multiple times per frame, and for different reasons. You may want to look at either moving some of this code to Update(), or have it only fire on the Repaint event in OnGUI().
Ok, I put the method into OnGUI(). But when I start the game, I see lots of errors [they are reffered to ther rect], but the error is always the same:
NullReferenceException: Object reference not set to an instance of an object
How can I solve this problem? I attached a texture to the gameObject which contains this script. Thanks for your help.
Line 13 is the only line where I spot a potential NullReferenceException. First look in the Inspector pane and verify that 'rectTexture' is initialized. If it is, then double click on the error message to see what line is having the issue. If it is line 13, go to the Hierarchy pane and type the name of the script into the search box in the upper right corner of the pane. You may have the script attached to another game object as well.
Ok, thanks, now the failure does not appear anymore, but I am still not able to draw a Rectangle.
private void SelectingObject()
{
if (Input.GetButtonDown ("Fire1"))
{
mousePos = Input.mousePosition;
print ("Fire1 down");
}
else if (Input.GetButtonUp ("Fire1"))
{
float[] difference = new float[2];
difference[0] = Input.mousePosition.x - mousePos.x;
difference[1] = Input.mousePosition.y - mousePos.y;
difference[1] = Screen.height - difference[1];
mousePos.y = Screen.height - mousePos.y;
Rect selectedRect = new Rect(mousePos.x, mousePos.y, difference[0], difference[1]);
GUI.color = Color.green;
GUI.DrawTexture (selectedRect, rectTexture, Scale$$anonymous$$ode.ScaleToFit);
Debug.Log ("Fire1 Up");
}
}
This is called at OnGUI();
Answer by immersiveGamer · Feb 23, 2014 at 10:39 AM
Like robertbu said you need to call the SelectingObject function inside of OnGUI in your script. The problem is that where you have the rectangle drawing it will only draw for one frame, or not at all since it is only running when you left off the button press. I used a bool to control when the texture is drawn. Tried to fix the math but I don't have the time to figure it out. Also you might want to have the scale set to stretch. Anyways, here is my code:
Vector2 mousePos;
public Rect selectedRect;
public Texture2D rectTexture;
//bool to turn on and off the texure.
bool show = false;
//this will be called by unity
void OnGUI()
{
SelectingObject();
}
void SelectingObject()
{
if (Input.GetMouseButtonDown(0))
{
mousePos = Input.mousePosition;
print (mousePos);
show = true;
}
else if (Input.GetMouseButtonUp(0))
{
float[] difference = new float[2];
difference[0] = Input.mousePosition.x - mousePos.x;
difference[1] = Input.mousePosition.y - mousePos.y;
difference[1] = Screen.height - difference[1];
mousePos.y = Screen.height -mousePos.y;
selectedRect = new Rect(mousePos.x, mousePos.y, difference[0], difference[1]);
GUI.color = Color.green;
show = false;
}
//if true show the texture
if(show)
GUI.DrawTexture (selectedRect, rectTexture, ScaleMode.StretchToFill);
}
Answer by Fevenius · Mar 04, 2014 at 01:12 PM
Hello there, now I have another problem with Unit-Selectiíon: I have a method called 'Move'. This method should move every selected GameObject to the position of the mouse by using the CharacterController.Move() function. Here is the code:
void Move()
{
List<GameObject> selectedObjects = SelectedObjects();
if (selectedObjects.Count != 0)
{
foreach (GameObject gameObj in selectedObjects)
{
if (Input.GetMouseButtonDown(1))
{
RaycastHit hit;
Vector3 target = Vector3.zero;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray,out hit))
{
target = hit.point;
}
target = target - gameObj.transform.position;
gameObj.GetComponent<CharacterController>().SimpleMove(target * Time.deltaTime);
}
}
}
}
I already tested if the code works when I directly change the GameObject's position to the position of the mouse, that worked. But why does the move method not work correctly?