Using Unity 5.6 C#…
I’m having trouble understanding the “get; set;” function. I’m using interfaces, but
they only seem to convey ‘strings’ ‘ints’ and ‘bools’. What I really need is Vector3’s and List conveyed.
Not only a List of GameObjects, but a way to set said objects 'new Vector3’s…
Now I’ve read some people who say Interfaces are useless, beginner only code. Or more clearly the ‘get; set;’ function.
I will try to show only necessary code, with errors, and what I’m trying to do as clearly as possible:
public class MainCameraControl : MonoBehaviour, IAmSelected
{
IAmSelected selected; //Interface calling bool AmSelected, List<GameObject> selectedUnits
public List <GameObject> selectedUnits { get; set;}
//public List<GameObject> selectedUnits = new List<GameObject>();
public bool AmSelected { get; set;}
ObjectRayHit = hit.collider.gameObject;
if(ObjectRayHit.tag == "Units"){
selected = (IAmSelected)gameObject.GetComponent("IAmSelected");
selected.selectedUnits.Add (ObjectRayHit); //This doesn't work
//selectedUnits.Add(ObjectRayHit); This used to work with 'new List declaration'
}
if (selected.selectedUnits.Count > 0) { //This does not work
figureCentroid ();
foreach (GameObject unit in selectedUnits){
selected = (IAmSelected)unit.GetComponent("IAmSelected");
selected.AmSelected = true; //This did work in UnitClass with 'new List declaration'
}
}
public class MoveToFormation : MonoBehaviour, IAmSelected, IUnitInfo
{
public Vector3 locatorPosition { get; set; }
IAmSelected selected;
public bool AmSelected { get; set;}
public List<GameObject> selectedUnits { get; set; }
// I can't get anything to call or send any of this info below
// Foreach of selectedUnits doesn't work, and when it did
// I get the Error that "it.gameObject no definition for locatorPosition"
// Why can I not send that Vector3 with unit in UnitClass???
// Code may look messed up from hundreds of debug's, lol...
if (selected.selectedUnits.Count > 0)
{
total = selectedUnits.Count;
formation = iterate - (calcLine * rowCount);
foreach (GameObject it in selected.selectedUnits) {
if (selectedUnits.IndexOf(it) == iterate) {
if (North == true) {
it.locatorPosition.z = (gameObject.transform.position.z - (spacing * rowCount));
it.locatorPosition.x = (gameObject.transform.position.x + (spacing * formation)) - calcLine;
it.North = true;
}
if (East == true) {
it.locatorPosition.z = (gameObject.transform.position.z + (spacing * formation)) - calcLine;
it.locatorPosition.x = (gameObject.transform.position.x - (spacing * rowCount));
it.East = true;
}
if (South == true) {
it.locatorPosition.z = (gameObject.transform.position.z + (spacing * rowCount));
it.locatorPosition.x = (gameObject.transform.position.x + (spacing * formation)) - calcLine;
it.South = true;
}
if (West == true) {
it.locatorPosition.z = (gameObject.transform.position.z + (spacing * formation)) - calcLine;
it.locatorPosition.x = (gameObject.transform.position.x + (spacing * rowCount));
it.West = true;
}
iterate++;
}
}
}
}
public class UnitClass : MonoBehaviour, IAmSelected, IUnitInfo
{
IAmSelected selected;
public Vector3 locatorPosition { get; set; } // IUnitInfo interface should carry this over, but doesn't
public bool AmSelected { get; set; }
public List<GameObject> selectedUnits { get; set; }
foreach (GameObject it in selected.selectedUnits) {
Debug.Log ("it " + it.name); // Only get errors about selectedUnits List
}
// When I debug, even in a simple code move, locatorPosition is always 0.0x, 0.0z
// locatorPosition Ypos is set to Unit, for terrain differences
locatorPosition = new Vector3(locatorPosition.x, gameObject.transform.position.y, locatorPosition.z);
float dist = Vector3.Distance (gameObject.transform.position, locatorPosition);
gameObject.transform.LookAt (locatorPosition); // waypoint.transform.position
gameObject.transform.position += gameObject.transform.forward * speed * Time.deltaTime;
}
I just can’t GET the list to work in any class, and can’t SET any Vector3’s where I want them. There’s got to be a trick to this
I’m not understanding clearly. If anyone can even slightly point me in the right direction, you are the coding master!