How to create new for my custom class?
Hi . I want to use ny new class like
new Relation(a string,an int, another int);
but when i did this it returns null
[Serializable]
public class Relation
{
public int id;
public string name;
public int value;
public Relation(int id, string name, int value)
{
}
}
Answer by spooneystone · Dec 08, 2016 at 03:28 PM
Give this a go.
[Serializable] public class Relation { public int id; public string name; public int value; public Relation(int _id, string _name, _int value) { _id = id; _name = name; _value = value; } }
when i create a new Relation(), it doesnt use my values and string became null, int's became 0.
Answer by SoraMahiro · Dec 08, 2016 at 05:19 PM
Honestly, creating a class to be used is far less efficient than using things like polymorphism or good ol' inheritance. However, when you create a new script, you create a new class that can be used at any time as long as you have a reference to that script. Although here is something you can do by using Polymorphism that may help.
using System;
using UnityEngine;
[Serializable] public class Parent:MonoBehaviour {
//some code and functions
}
Then,
using System;
using UnityEngine;
public class Child:Parent {
//some code and functions
}