- Home /
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
Your answer
Follow this Question
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