x


C# Utility Class

I'll admit that I'm completely new to C# and Unity3D. I am coming from an ActionScript 3.0 background, where I can create a class file that can then be instantiated from within other classes, as in:

var foo:Bar = new Bar();

Normally, I'd write a class to handle specific tasks, like loading and parsing an XML file, and I'd be able to instantiate it where needed to perform its duty. I'm trying to do the same with a tiny XMLParser class that I found on the forums here.

My question, then, is: How do I use this? I can't seem to just place it anywhere in the Project folder and instantiate it. Placing it in the Plugins folder or the Standard Assets folder makes no difference. I can't place it on a GameObject because it doesn't inherit from MonoBehavior, and I know that I could edit it to do so, but what's the point? If the code was provided like this I'm assuming that they wrote it to work this way, and I just have no idea how to use it properly.

So. I want to write a utility class, as in:

using UnityEngine;
using System.Collections;

class XMLParser {
    XMLParser() { }
    Foo ParseString(string xmlString) {
       // parse
       Foo parsedXML = Bar;
       return parsedXML;
    }  
}

Is there something I'm totally missing on how to do this? or is this just not how Unity3D works?

more ▼

asked Jun 07 '11 at 07:10 PM

cr0ybot gravatar image

cr0ybot
1 1 1 3

I too am interested in this. The other night I was wanting something like a "Team", but it doesn't exactly make sense for it to make a GameObject because it's just an abstract association. Where and how would I get "team" code executed? (Like tracking kills, being responsible for relaying messages possibly. Things like that.)

Jun 07 '11 at 07:23 PM flaviusxvii
(comments are locked)
10|3000 characters needed characters left

1 answer: sort newest

You should just be able to put the .cs file anywhere in your Assets folder, and instantiate it (that is, new it, not Object.Instantiate() it) from within your own code. What error are you getting?

more ▼

answered Jun 07 '11 at 07:27 PM

sneftel gravatar image

sneftel
1.7k 7 9 20

I was getting this: "error CS0122: 'XMLParser.XMLParser()' is inaccessible due to its protection level". I found that if I put "public" in front of the constructor and the ParseString method everything seems to work.

The .cs file is not in a special folder, and I am able to "new" it. Notice the "public" keywords that were missing from the original code I found:

using UnityEngine; using System.Collections;

class XMLParser { public XMLParser() { } public Foo ParseString(string xmlString) { // parse Foo parsedXML = Bar; return parsedXML; }
}

(Sorry about the formatting... couldn't see any way to do it in a comment)

Jun 07 '11 at 07:59 PM cr0ybot
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x4179
x1683
x339
x256
x10

asked: Jun 07 '11 at 07:10 PM

Seen: 2195 times

Last Updated: Jun 07 '11 at 08:00 PM