Class Creation

Hi All

I am trying to write my first application with Unity and while enjoying it I have hit a bit of a brick wall with class creation.

I have three scripts:

  1. the first reads a csv file into an array and calls the class constructor
  2. the second defines the class and has the class constructor
  3. the third searches the classes.

The third script is attached to a different object to the first two.

The probelm I am having is creating a class that has the name from the array and that can be called from another object.

Roughly scripts look like(the actual scripts are very long as the class has a lot of details):

script 1

array [name, detail, detail2] with many rows

for
personName = array[0];
personDetails = array[1];
personDetails2 = array[2];

script2.createPerson(personName, personDetails, personDetails2);

script2

class person
{
public var detail : string =“text”;
public var detail2 : int = 2;
}

static function createPerson (name, detail, detail2)
{
???
}

script3

if (name.detail == something)
{
do something;
}

So the question is how do I use createPerson to create a public object I can see from anywhere which looks like
person.detail
person.detail2

I have tried

CreatePerson (name, detail, detail2){
name= new createPerson();
name.birthplace = detail;
name.age = detail2;
}

But I cant then see “Luke.birthplace”.

Any advice on either how to achieve this or a better way to do it would be really appreciated. I can post full functions if it helps but they are quite long.

i think you need to add your person class, (which inherits from MonoBehaviour) to a gameObject

        GameObject go = new GameObject();
        go.name = // the name you extract from csv
        Person newPerson = go.AddComponent("Person") as Person;
        newPerson.detail = // the detail
        newPerson.detail2 = // the detail2
// add newPerson to array

this is c# but i’m sure you get the picture

hope this helps

if not, post the csv and i will get you started

From your explanation I don’t understand what are you trying to accomplish. From your explanation and pseudo code I see no reason why you should put the data into Unity managed objects (eg something that extends MonoBehaviour). Solving this task as general C# (or Javascript) program would probably be much easier. After that you should think how do you want your data to be used in Unity. There are quite a few ways (through components, prefabs, gameobjects, ?). If you give more info about how you want to use it in Unity, then we can help better.

Thanks both for responding so quickly.

The aim of the project is to display information about art, who made it, when, themes of the work etc. I load up the pictures (pic1, pic2, pic3 etc) on the screen then below have some text fields showing detials about the art. Previously I read of the array I had made from the csv file i.e I knew artist second name was colum 4 so for pic 8 I could goto array[8][4]. I was aware this was not ideal so thought I could make a class for picture
picture.name,
picture.date
picture.gallery

which I could then use both to get information and to search for criteria matches.

the first few rows from the CSV file are:

pic1,Fruit 4,21,2009,John,Doe,Mixed Media,75,55,0,0,1,1,1,0,0,0
pic2,Picture of vase,16,1582,Local,School,Oil Painting,50,30,1,0,0,0,0,0,0,0
pic3,Self portrait,21,111,Sally,Dally,Water-colour,57,35,0,0,0,1,0,0,0,0
pic4,Mountain,20,1938,Fred,Stone,Oil Painting,100,80,0,0,0,1,0,0,0,0

The numbers at the end are booleans for different themes.

Try this as a start

Gallery.cs
Add to gameObject and attach csv

using UnityEngine;
using System.Collections;
using System.Text.RegularExpressions;

public class Gallery : MonoBehaviour {

    public TextAsset m_csv;
    public TextAsset csv{
        get{return m_csv;}
        set {m_csv=value;}
    }
    
    public Artwork[] m_artwork;
    public Artwork[] artwork{
        get{return m_artwork;}
        set {m_artwork=value;}
    }
    
    void Start () {
        ArrayList aRtwork = new ArrayList();
        Regex regex = new Regex(
            "(.+),(.+),(\\d+),(\\d+),(.+),(.+),(.+),(\\d+),(\\d+),(\\d+),(\\d+),(\\d+),(\\d+),(\\d+),(\\d+),(\\d+),(\\d+)"
        );
        MatchCollection matches = regex.Matches(csv.text);
        
        foreach(Match match in matches){
            // instantiate artwork
            GameObject go = new GameObject();
            go.transform.parent = transform;
            go.name = match.Groups[1].Value;
            
            Artwork newArt = go.AddComponent("Artwork") as Artwork;
            
            // apply data to artwork
            newArt.description = match.Groups[2].Value;
            newArt.year = System.Convert.ToInt32( match.Groups[4].Value );
            
            newArt.artist = new Artist();
            newArt.artist.name = match.Groups[5].Value;
            newArt.artist.surname = match.Groups[6].Value;
            newArt.medium = match.Groups[7].Value;
            
            // load texture from resources
            newArt.image = Resources.Load( match.Groups[1].Value , typeof(Texture2D) ) as Texture2D;
            
            // apply more data here
            
            // add artwork to gallery
            aRtwork.Add(newArt);
        }
        
        // convert arrylist to Artwork[]
        artwork = aRtwork.ToArray(typeof(Artwork)) as Artwork[];
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

Artwork.cs

using UnityEngine;
using System.Collections;

public class Artwork : MonoBehaviour {
    
    public Texture2D m_image;
    public Texture2D image{
        get{return m_image;}
        set {m_image=value;}
    }
    
    public string m_description;
    public string description{
        get{return m_description;}
        set {m_description=value;}
    }
    
    public int m_year;
    public int year{
        get{return m_year;}
        set {m_year=value;}
    }
    
    public Artist m_artist;
    public Artist artist{
        get{return m_artist;}
        set {m_artist=value;}
    }
    
    public string m_medium;
    public string medium{
        get{return m_medium;}
        set {m_medium=value;}
    }

	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

Artist.cs

using UnityEngine;
using System.Collections;

[System.Serializable]
public class Artist : System.Object {

	public string m_name;
    public string name{
        get{return m_name;}
        set {m_name=value;}
    }
    
    public string m_surname;
    public string surname{
        get{return m_surname;}
        set {m_surname=value;}
    }
    
}

wow thanks for doing all that, really appreciated. You’ve even used regex which I’d read about but had been to confused to use - really nice to see it implimented. I’ll be going through your code tonight!!

Thanks again