Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Fevenius · Feb 22, 2014 at 09:09 PM · selecting

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...

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kiloblargh · Feb 22, 2014 at 11:14 PM 0
Share

Is this a 2d ( straight top-down) game? If not, your whole approach is wrong.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

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().

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Fevenius · Feb 22, 2014 at 09:26 PM 0
Share

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.

avatar image robertbu · Feb 22, 2014 at 10:49 PM 0
Share

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.

avatar image Fevenius · Feb 23, 2014 at 09:13 AM 0
Share

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();

avatar image
0

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);
     }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity RayCast Selection 1 Answer

Standard assets - Select what to import. 0 Answers

C# adding items to an already created Array 4 Answers

how to destroy two game objects when selected 0 Answers

Selecting an object on click, then clicking a UI button to change its color. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges