Question

How do I add an element to an Generic List?

I created a button in my AddAssignment Script, when I click it, I want to add an element to the assignment list. The element that is added should be my AssignmentClass.

Sorry if this is a lot of code to look through.

//AddAssignment Script

 #pragma strict
import System.Collections.Generic;

var myTransform : Transform;

var windowOpen : boolean;

var titleText = "title";
var deadlineText = "deadline";
var adressText = "adress";
var cityText = "city";

var baseScript : BaseScript;

function Start () 
{
    myTransform = transform;
    baseScript = myTransform.GetComponent(BaseScript);
}


function Update () 
{
    if (Input.GetKeyUp ("p"))
    {
        windowOpen = !windowOpen;
    }
}


function OnGUI ()
{
    if (windowOpen == true)
    {
        GUI.BeginGroup (new Rect (25, 25, 200, 200));
        GUI.Box (new Rect (0, 0, 200, 175), "");

            //new info
            titleText = GUI.TextField (Rect (25, 25, 100, 25), titleText);
            deadlineText = GUI.TextField (Rect (25, 50, 100, 25), deadlineText);
            adressText = GUI.TextField (Rect (25, 75, 100, 25), adressText);
            cityText = GUI.TextField (Rect (25, 100, 100, 25), cityText);

            //add with new info
            if (GUI.Button (new Rect (25, 130, 150, 25), "Add Assignment"))
            {
                //baseScript.assignments.Add();
            }

        GUI.EndGroup();
    }
}

//BaseScript

#pragma strict
import System.Collections.Generic;

var assignments : List.<Assignments> = new List.<Assignments>();
var scrollPosition : Vector2 = Vector2.zero;


function OnGUI ()
{
    GUI.Box (new Rect (0, 0, Screen.width, Screen.height), "");


    var yOffset = 0;
    for (var i in assignments)
    {

    scrollPosition = GUI.BeginScrollView (Rect (0, 0, Screen.width, Screen.height), 
                            scrollPosition, Rect (0, 0, Screen.width - 25, 75 + yOffset));

        if (GUI.Button (new Rect (Screen.width / 2 - 120, 25 + yOffset, 200, 50), i.title))
        {
            i.running = !i.running;
            Debug.Log(i.title);
        }
        if (GUI.Button (new Rect (Screen.width / 2 + 85, 25 + yOffset, 35, 50), "X"))
        {
            Debug.Log ("removed " + i.title);
            assignments.Remove(i);
        }
        yOffset += 55;

    GUI.EndScrollView ();
    }
}

//AssignmentClass Script

#pragma strict

class Assignments 
{
    var running : boolean;
    var title : String;
    var deadline : String;
    var adress : String;
    var city : String;
}
Was it helpful?

Solution

I figured this out myself. In case someone is curious as to how i did it, here it is: I simply had to declare my class before i could use it. feeling stupid :D

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top