Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by cr0ybot · Jun 07, 2011 at 07:10 PM · c#instantiateclassxmlutility

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?

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image flaviusxvii · Jun 07, 2011 at 07:23 PM 0
Share

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 "$$anonymous$$m" code executed? (Like tracking kills, being responsible for relaying messages possibly. Things like that.)

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by sneftel · Jun 07, 2011 at 07:27 PM

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?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image cr0ybot · Jun 07, 2011 at 07:59 PM 0
Share

I was getting this: "error CS0122: 'X$$anonymous$$LParser.X$$anonymous$$LParser()' 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 X$$anonymous$$LParser { public X$$anonymous$$LParser() { } public Foo ParseString(string xmlString) { // parse Foo parsedX$$anonymous$$L = Bar; return parsedX$$anonymous$$L; }
}

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Behind the scenes voodoo w/ Instantiate 2 Answers

Distribute terrain in zones 3 Answers

Instantiate class from other script but same gameObject 3 Answers

Multiple Cars not working 1 Answer

How to instantiate gameobjects through udp? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges