I am new to programming and struggling with trying to develop solid OO programming principles from the start.
I made a project that puts 2d game objects on the screen and each game object has properties such as cityname, population and position.
I want the user to hover over a city and on the top it will display info about that city. The code works, but I have a suspicion that there is a much simpler way of doing this without using the costly foreach loop in the update section of initialize script. I know it doesnt really matter for a small program like this but I think it will be a real problem if my scope starts to expand.
here are the scripts
using UnityEngine;
using System.Collections;
public class initialize : MonoBehaviour
{
public Transform thiscity;
public Transform bracket;
private Transform mybracket;
int layerMask = 1 << 8;
Ray ray;
void Awake ()
{
CITY1 a = new CITY1 ("Las_Vegas", 10000, -49.8f, 4.9f);
CITY1 b = new CITY1 ("Berkley", 240000, -39.6f, 7f);
GameControl.control.cities.Add (a);
GameControl.control.cities.Add (b);
int count = GameControl.control.cities.Count;
for (int i=0; i< count; i++) {
var go = Instantiate (thiscity, new Vector3 (GameControl.control.cities [i].x, GameControl.control.cities [i].y, -4f), Quaternion.identity);
go.name = GameControl.control.cities [i].cityLabel;
}
mybracket = Instantiate (bracket, new Vector3 (GameControl.control.cities [0].x, GameControl.control.cities [0].y, -4f), Quaternion.identity)as Transform;
}
void Start()
{
}
// Update is called once per frame
void Update ()
{
Vector2 cameraPosition = new Vector2 (Camera.main.transform.position.x, Camera.main.transform.position.y);
RaycastHit2D hit = Physics2D.Raycast (Camera.main.ScreenToWorldPoint (Input.mousePosition), Vector2.zero, layerMask);
if (hit != null && hit.collider != null) {
GameControl.cityname = hit.collider.gameObject.name; //if mouse hover, put name of gameobject into gamecontrol.cityname
}
foreach (CITY1 mycity in GameControl.control.cities)
if (mycity.cityLabel == GameControl.cityname) {
string population = mycity.population.ToString ();
string nnn = mycity.cityLabel.ToString ();
GameControl.cityinfo = "Population is " + population + "\nName is " + nnn;
mybracket.position= new Vector3(mycity.x,mycity.y,-4f);
break;
}
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
//this is the common class inbetween scenes that rentain its value.
public class GameControl : MonoBehaviour {
public static GameControl control;
public float health;
public string currentco;
public float experience;
public int population;
public int CO_number;
//public List<Inside_CO> my_co = new List<Inside_CO>();
public List<CO1> my_co3 = new List<CO1>();
public List<CITY1> cities = new List<CITY1>();
// public List<CO1> my_co1 = new List<CO1>();
public string player_label;
//public CO my_co2;
// public static bool showcity;
public static int money3;
//public static string cityname;
public static string cityinfo;
public static string cityname;
void Awake() {
if (control ==null)
{
DontDestroyOnLoad(gameObject);
control=this;
}
else if (control !=this)
{
Destroy(gameObject);
}
}
}
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class CITY1 {
public string cityLabel ;//{ get { return coLabel; } }
public int population;
public float x;
public float y;
public CITY1 (string city_label,int pop_label, float x_val, float y_val)
{
cityLabel=city_label;
population= pop_label;
x=x_val;
y=y_val;
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class TextScript1 : MonoBehaviour
{
private Text text;
void Start ()
{
text = GetComponent <Text> ();
text.text = GameControl.cityinfo;
}
void Update ()
{
text.text = GameControl.cityinfo;
}
}