- Home /
getting values from class
hello i'm trying to retrieve variables from a class without success the foreach loop is ignored it seems any ideas?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class myscript : MonoBehaviour
{
class myclass
{
public string t;
public int r;
public int m;
public myclass(string newT, int newR, int newM)
{
t = newT;
r = newR;
m = newM;
}
}
List<myclass> mylist = new List<myclass>();
void Start()
{
mylist.Add(new myclass("0", 0, 0));
mylist.Add(new myclass("2", 0, 0));
mylist.Add(new myclass("3", 0, 0));
foreach (myclass m in mylist)
{
print("---check---");
print(m.t + " - " + m.r + " - " + m.m);
}
}
}
I don't see any problem here. You should be more specific about what you have tried and what actually happens. You haven't said anything about your setup. So are you sure that:
Your script is attached to a gameobject in the scene?
the gameobject where the script is attached to is active (not deactivated)
every parent of the gameobject is also active. The active state is inherited down the hierarchy
your script component is not disabled.
All those points can be verified at once by placing a Debug.Log / print at the very first line inside your Start method to see if Start is executed at all. If it does execute you should see your prints in the console.
Again you should add more details about your issue. What does the console show? Anything? $$anonymous$$aybe an error? If there are compiler errors, even in another script, your script won't work since all scripts are compiled into one assembly.
I simple copy past your code into unity and it works, as bunny asked, is your script attached to a gameobject in the scene?
Answer by juicyz · Jan 27, 2017 at 01:15 AM
Try 'Debug.Log' instead of 'print'
That won't change anything. "print" is a static method of the $$anonymous$$onoBehaviour class and is literally defined as:
public static void print(object message)
{
Debug.Log(message);
}
Your answer
Follow this Question
Related Questions
Foreach - what am I doing wrong? 1 Answer
Can't use method from custom class during foreach 2 Answers
Problem with button press 2 Answers
Overriding a function. 1 Answer