Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 NoCandyIncluded · Feb 17, 2020 at 05:14 AM · rtsselectionbox

Cant select objects using my box-selection script

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 using System;
 using System.Collections.Generic;
 
 public class DragSelection : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
 {
     public List<GameObject> selected = new List<GameObject>();
     public List<GameObject> selectable = new List<GameObject>();
 
     [SerializeField]
     Image selectionBoxImage;
 
     Vector2 startPostion;
     Rect selectionRect;
     
 
 
 
     public void OnBeginDrag(PointerEventData eventData)
     {
         if (Input.GetMouseButton(0))
         {
             selectionBoxImage.gameObject.SetActive(true);
             startPostion = eventData.position;
             selectionRect = new Rect();
             
         }
         
     }
 
     public void OnDrag(PointerEventData eventData)
     {
         if (Input.GetMouseButton(0))
         {
         //X axis
         if (eventData.position.x < startPostion.x)
         {
             selectionRect.xMin = eventData.position.x;
             selectionRect.xMax = startPostion.x;
         }
         else
         {
             selectionRect.xMin = startPostion.x;
             selectionRect.xMax = eventData.position.x;
         }
         //Y axis
         if (eventData.position.y < startPostion.y)
         {
             selectionRect.yMin = eventData.position.y; 
             selectionRect.yMax = startPostion.y;
         }
         else
         {
             selectionRect.yMin = startPostion.y;
             selectionRect.yMax = eventData.position.y;
         }
 
         selectionBoxImage.rectTransform.offsetMin = selectionRect.min;
         selectionBoxImage.rectTransform.offsetMax = selectionRect.max;
             
         }
         
     }
 
     public void OnEndDrag(PointerEventData eventData)
     {
         
         
         selectionBoxImage.gameObject.SetActive(false);
         foreach (GameObject selectObject in selectable)
         {
             if (selectObject != null)
             {               
                 Debug.Log("object found");
                 
                 if (selectionRect.Contains(Camera.main.WorldToScreenPoint(selectObject.transform.position)))
                 {
                     selected.Add(selectObject);
                     Debug.Log("object selected");
                 }
             }
             
         }
            
     }
 
 
 }
 

im trying to make a box select feature in my game, and in the OnEndDrag method, I am trying to find all the selectable gameobjects in the box selector and add them to the selected list(Yes, I added gameobjects to the selectable list). the only error it gave me was:

NullReferenceException: Object reference not set to an instance of an object DragSelection.OnEndDrag (UnityEngine.EventSystems.PointerEventData eventData) (at Assets/AI Project/DragSelection.cs:79) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IEndDragHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:78) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261) UnityEngine.EventSystems.EventSystem:Update()

LINE 79: if (selectionRect.Contains(Camera.main.WorldToScreenPoint(selectObject.transform.position))); This got me stumped because I cant seem to figure out what object reference isn't set to an instance of an object. Usually I never have problems figuring out stuff like this but this time isn't the case. NOTE: this is my first time using the IBeginDragHandler, IDragHandler, and IEndDragHandler interfaces in a script, so I could have a lot wrong.
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by NoCandyIncluded · Feb 17, 2020 at 10:17 PM

Bumping because I still cant find an answer

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

123 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Create selection box by double tapping then dragging? (android mobile)(cnc style)(image embedded) 1 Answer

Click and Drag RTS Mouse selecting script help. 1 Answer

C# converting Input.Mouseposition.y to to "top" argument of GUI.Box 1 Answer

How to make free area selection(like lasso tool in photoshop) in rts? 2 Answers

Strange Behavior with RTS-Style Selection Box and Moving Camera 0 Answers


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