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 /
avatar image
0
Question by Favonius · Apr 18, 2016 at 01:28 PM · 2d gamelist2d-gameplayselectlist of lists

How to select and deselect all objects from a list from another class?

So I have two script in my 2D game. A HomeBase script and a UnitControllerScript, where the HomeBase creates a list of units with the UnitControllerScript attached to them. So what I'm trying to do is when I click on a unit, I want to select every instance of units from that same list. How can I achieve that? Here are the two classes? Also what is the best way to select and deselect?

 using UnityEngine;
 using System.Collections.Generic;
 using System.Collections;
 
 
 public class HomeBase : MonoBehaviour {
 /// <summary>
 /// Allied base class
 /// </summary>
 /// 
 //Holds all the variables for base
 public GameObject bases;
 public GameObject tile;
 public GameObject playerUnit;
 public int garrisonSize;
 LevelManager levelManager;
 GameObject levelAccess;
 public List<GameObject> soldierUnit;
 public int health;
 public SpriteRenderer color;
 private float startTime;
 private bool isDestroy;

 float respawnCoolDown;


 // Use this for initialization
 void Start()
 {
     isDestroy = false;
     garrisonSize = 10;
     soldierUnit = new List<GameObject>();

     //Spawns starting units
     for (float i = 0; i < garrisonSize; i++)
     {
         float num = (i * 1) / 10;
          
         float angle = num * Mathf.PI * 2;

         float x = Mathf.Sin(angle) * 2;
         float y = Mathf.Cos(angle) * 2;

         Vector3 point = new Vector3(x, y, 0) + this.transform.position;

         GameObject newUnit = Instantiate(playerUnit, point, Quaternion.identity) as GameObject;
         soldierUnit.Add(newUnit);
         newUnit.GetComponent<UnitController> ().Initialize (this);

     }



     //Instantiate(tile, new Vector2(bases.transform.position.x, bases.transform.position.y), Quaternion.identity);
     color = GetComponent<SpriteRenderer>();

 }

 // Update is called once per frame
 void Update () {

     float elapsed = Time.time - startTime;

     
     InvokeRepeating("Respawn",1f,5f);
     if (isDestroy == true) {
         for (int i = 0; i < soldierUnit.Count; i++) 
         {
             if (soldierUnit [i] == null) {
                 soldierUnit.RemoveAt (i);

                 isDestroy = false;
             }
         }
     }

 }

 void OnCollisionEnter2D(Collision2D collider)    
 {
     if(collider.gameObject.tag == "Player")
     {
         Debug.Log("Player unit lenght" + soldierUnit.Count);
     }
 }

 public void Respawn()
 {
     if(soldierUnit.Count<garrisonSize)
     {
         for (float i = soldierUnit.Count; i < garrisonSize; i++)
         {
             float num = (i * 1) / 10;

             float angle = num * Mathf.PI * 2;

             float x = Mathf.Sin(angle) * 2;
             float y = Mathf.Cos(angle) * 2;

             Vector3 point = new Vector3(x, y, 0) + this.transform.position;

             GameObject newUnit = Instantiate(playerUnit, point, Quaternion.identity) as GameObject;
             soldierUnit.Add(newUnit);

         }
     }
 }
 public void UnitChecker()
 {
     
     isDestroy = true;

 }

}

 using UnityEngine;
 using System.Collections;
 
 public class UnitController : MonoBehaviour {
 
     //Variables
     public float moveSpeed;
     public float moveVelocity;
     private Vector3 target;
     private float health;
     bool active = true;
     bool isSelected = false;
 
     //Holds positions
     private Vector3 mousePosition;
     private Vector3 playerPosition;
     private float targetDistance;
 
     //Rigid body
     private Rigidbody2D rBody;
 
     //Sprite info
     SpriteRenderer color;
     private Quaternion facing;
 
     //Access variables
     PlayerController playerAccess;
     HomeBase homeBase;
     private Transform thisTransform;
 
     private bool notMove;
 
     public void Initialize(HomeBase homeObject)
     {
         homeBase = homeObject;
     }
     // Use this for initialization
     void Start () {
     
         playerAccess = GetComponent<PlayerController>();
         thisTransform = transform;
         rBody = GetComponent<Rigidbody2D>();
         color = GetComponent<SpriteRenderer>();
         color.color = new Color(1f, 0f, 1f, 1f);
         facing = transform.rotation;
         health = 10f;
     }
     
     // Update is called once per frame
     void Update () {
 
         //Reads if the user is clicking
         if (Input.GetMouseButtonDown (0)) 
         {
             //Cast a ray down to where the mouse clicks are
             RaycastHit hitInfo = new RaycastHit();
             bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo);
             //If it hits something
             if(hit)
             {
                 //Spits out the object's name
                 Debug.Log("Hit " + hitInfo.transform.gameObject.name);
                 //If the object is a base prints the debug message
                 if (hitInfo.transform.gameObject.tag == "Base")
                 {
                     Debug.Log ("It's working!");
                 } else {
                     Debug.Log (hitInfo.transform.gameObject.tag);
                 }
             } 
 
             //If it doesn't hit anything then say no hit
             else 
             {
                 Debug.Log("No hit");
             }
         }
 
         //Checks if health is zero to remove
         //if (health <= 0 && active == false) 
         //{
         //Destroy (this);
         //homeBase.UnitChecker(this.gameObject);
 
         //}
 
 
             Move();
     
     }
 
     public void Move()
     {
         //Sets the Velocity of the mouse
         moveVelocity = 0f;
 
         //If the user is clicking the left mouse
         if (Input.GetMouseButtonDown(0)&& isSelected)
         {
             //Sets target to the position of the mouse
             target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             //get the Z direction
             target.z = transform.position.z;
             //sets the object to be moving
             notMove = true;
 
         }
 
         //Checks if the object is not moving
         if (notMove == true)
         {
             //Move the object's position toward the target location
             transform.position = Vector3.MoveTowards(transform.position, target, moveSpeed * Time.deltaTime);
             //Get the direction of the mouse click
             Vector3 dir = mousePosition - transform.position;
             //Gets the angle calculation to rotate the object to the direction it is moving to
             float angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
             //Rotate the sprite
             transform.rotation = Quaternion.LookRotation(Vector3.forward);
             transform.rotation = Quaternion.AngleAxis(angle + 90.0f, Vector3.forward);
 
             //If the object reaches the target position sets the notMove to false to stop moving
             if (transform.position == target)
             {
                 notMove = false;
             }
         }
 
     }
 
 
     void OnCollisionEnter2D(Collision2D other)
     {
         if (other.gameObject.tag == "EnemyUnit")
         {
             Destroy (gameObject);
             Destroy (other.gameObject);
             homeBase.UnitChecker();
             //Destroy(this.gameObject);
         }
     }
 
     void OnMouseDown()
     {
 
         isSelected = true;
     }
     
 }
 


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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

,Spawning snow or changing tilesets to snow as the player walks past them 0 Answers

How could I disable a click on a sprite when player is moving? 1 Answer

simple 2d controller with a and d. 1 Answer

How can I stop hurt animation and bounce the player when hit by an enemy? 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