- Home /
How do I edit variables in an array of a class??
Ok hello um, I've successfully made an array of System. Object in the code below and can edit countless instances of the class Test's variables in the editor however since it's basically my first time threading in these apparently deep waters I have no idea how to modify or access the variables in Test via code, I'm competent in unity script but not so much in c#. I'd very much appreciate it if someone could show or explain how I'd go about doing that.
class Test extends System.Object {
var p = 5;
var c = Color.white;
}
var test = Test ();
Answer by Baste · Nov 21, 2014 at 10:16 AM
First of all, 'extends System.Object' is redundant. Every class always extends Object - if you don't specify what class you're extending, you're still extending Object. If you're extending MonoBehaviour: supprise! MonoBehaviour inherits from Object, or some other class that extends Object.
Now, if you want to edit one of the variables of the test object, there's one huge difference between UnityScript and C#. In UnityScript, variables are public as of default. So when you write 'var p = 5' in the top of a UnityScript script that variable is available outside of that particular class (script)
In C#, variables are private as of default. Which means that to access them outside of the same class (script), you have to declare them as public.
As an example, assume you have to scripts; A and B:
class Foo {
public var a = 5;
var b = 3;
}
class Bar : MonoBehaviour {
public Foo theFoo;
void Start() {
//this will work:
Debug.Log(theFoo.a);
//this will not work, as b is a private variable, only accessible inside of the class Foo:
Debug.Log(theFoo.b);
}
}
That'll give you a start, but you should really try to look at some tutorials. The official ones are okay.
Answer by psycocrusher · Nov 21, 2014 at 07:19 PM
Try this, you need to call system and Serializable or they won't appear in the inspector:
using UnityEngine;
using System.Collections;
using System;
public class Splash : MonoBehaviour {
public static string VersionNumber;
public static bool IsFullVersion;
public Version SetVersion;
[Serializable]
public class Version : System.Object {
public string VersionNumber;
public bool FullVersion;
}
void Awake (){
VersionNumber = SetVersion.VersionNumber;
IsFullVersion = SetVersion.FullVersion;
}
Your answer
Follow this Question
Related Questions
Build problem, missing variables 1 Answer
In-Game Console and Permanent Variable Changes 1 Answer
Is there a list of variable attributes for C#? 2 Answers
Problem storing GameObject reference in script - PUN 0 Answers
how can i write this properly 3 Answers