Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Ego65 · Nov 15, 2016 at 12:32 PM · cameraraycasthitselectionmouse positionrectangle

how to read in game objects in a list from different script

hey, i'm following a new tut for a RTS game. But there's only explained how to select each single object. So i fighted me through several tuts, and found a script attached to a unit prefab which detects if it's selected by drawing a rectangle for multi selection.
It basically works but i haven't found a way to read in those units in a List in a script which is attached to the maincam with witch the rectangle is drawn and there i declared the List.

This script is used with the main cam :

 public Texture aTexture;
 public static Rect selection = new Rect(0,0,0,0);
 private Vector2  _box_start_pos = Vector2.zero;
 private Vector2 _box_end_pos = Vector2.zero;
 public string Pl_tag = "Player"; 
 public List<GameObject> selectedObj = new List<GameObject>();
 
 void Update ()
 {
 Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit; 
         Debug.DrawRay(ray.origin, ray.direction * 100.0f, Color.yellow);
 // Called while the user is holding the mouse down.
         if(Input.GetKey(KeyCode.Mouse0))
         {
             if (Physics.Raycast(ray, out hit ))
             {
                 //Debug.Log(hit.collider.tag  );
                 if(hit.collider.tag == Pl_tag)
                 {
                     var obj = hit.collider.gameObject;
 
                     if(selectedObj.Contains(obj) == false)
                     {
                         selectedObj.Add(obj);
                        
                     }
                     Debug.Log(Pl_tag +" hit");
                 }
             }
  else
             {
                 selectedObj.Clear();
                 Debug.Log("");
             }
 // Called on the first update where the user has pressed the mouse button.
             if (Input.GetKeyDown(KeyCode.Mouse0))
                 _box_start_pos = Input.mousePosition;
             else  // Else we must be in "drag" mode.
                 _box_end_pos = Input.mousePosition;    
         }
         else
         {
             // Handle the case where the player had been drawing a box but has now released.
             if(_box_end_pos != Vector2.zero && _box_start_pos != Vector2.zero)
                 //HandleUnitSelection();
 
             // Reset box positions.
             _box_end_pos = _box_start_pos = Vector2.zero;
         }
 /// <summary>
     /// Draws the selection rectangle if the user is holding the mouse down.
     /// </summary>
     void OnGUI()
     {
         // If we are in the middle of a selection draw the texture.
         if(_box_start_pos != Vector2.zero && _box_end_pos != Vector2.zero)
         {
             // Create a rectangle object out of the start and end position while transforming it
             // to the screen's cordinates.
             selection = new Rect(_box_start_pos.x, Screen.height - _box_start_pos.y,
                                   _box_end_pos.x - _box_start_pos.x,
                                    -1 * (_box_end_pos.y - _box_start_pos.y));
             // Draw the texture.
             GUI.DrawTexture(selection, aTexture);
         }
     }
  public static float InvertMouseY(float y)
         {
             return Screen.height - y;
         }  



This script is attached to the GameObject prefab called Unit:

    public GameObject  thistransform;
     public bool selected = false;
     private Renderer rend ;
    
 
     void Start()
     {
         Renderer rend = thistransform.GetComponent<Renderer>();
     }
     void Update () 
     {
        
         Renderer rend = thistransform.GetComponent<Renderer>();
         if(rend.isVisible && Input.GetMouseButtonUp(0))
         {
             Vector3 camPos = Camera.main.WorldToScreenPoint(transform.position);
             camPos.y = NewBehaviourScript.InvertMouseY(camPos.y);
             selected = NewBehaviourScript.selection.Contains(camPos, true);
         }
 
         if(selected)
         {
             rend.material.color = Color.red;  
         }
         else
             rend.material.color = Color.green;
        
     }  

So i would like to achiefe to read this game objects in the List selectedObj. If i understand it right selection.Contains(camPos, true) does return only Vector3 of each object ? I've tried very much but i'm out of any idea now.
I hope that all isn't to confusing the way i explained it here (i'm german)

so many thanks previously !!!

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 Ego65 · Nov 16, 2016 at 03:21 PM

I kept on trying and i found a solution with the "FindGameObjectsWithTag" command. Therefore i tagged a few units with "Player".
In the first script attached to the maincam i'added :

 public string Pl_tag = "Player";   
 public GameObject[] Arrayselobjects; 

 

In the Update() i've added :

    Arrayselobjects = GameObject.FindGameObjectsWithTag("Player"); // buildin array
     foreach(GameObject arrayselobj in Arrayselobjects )
     {
     bool isselected = arrayselobj.GetComponent<Unit>().selected;

         if(isselected)
         {
             if(selectedObj.Contains(arrayselobj) == false)
             {
                 selectedObj.Add(arrayselobj); // the objects in the array are now read in the List
             }
         }
     }  

At the end of the Update :

 Arrayselobjects = null; // this clears the array 

In the script for each Unit i changed :

   if(selected && gameObject.tag == "Player")
             {
                 rend.material.color = Color.red;  
             }
             else
             {
                 rend.material.color = Color.green;
             }  

So all in my test scene works well but i think its not the very best way for perfomance because using a Generic List and FindGameObjects costs a lot of perfomance !
All more advanced suggestions which aren't that expensive for perfomance are welcome ! :)

yours

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

100 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

Related Questions

checking which objects are within area on screen 0 Answers

selecting blocks on mouse click 2 Answers

How can I make an object move towards the camera when it's being looked at and go back to its original position when it's not longer being looked at? 0 Answers

Move Camera 2D Towards Mouse Position 0 Answers

Linecast not working properly for 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