Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by Platinium · Feb 20, 2013 at 02:40 PM · c#class

Call a class in c#

Hello, i`m having a problem that i can`t understand, for example in js i have:

//In a script named MenuElement i have:

 class MenuElements
 {
 
 //My public vars and enums methods etc
 
 }
 -------------------------------------------
 // And then in another Script named MenuWindows
 
 //code...
 var myElements: MenuElements[];

And this works perfectly, the problem is when i try to do the same thing in c# it simply doen`t work and gives error "NullReferenceException: Object reference not set to an instance of an object"

 // In script MenuElements
 public class MenuElements
 {
 //My public vars enums methods etc
 
 }
 
 //In Script MenuWindow
 
 public class MenuWindow : MonoBehaviour
 {
 
 public MenuElements[] myElements;

   void OnEnable()
  {
       if (myElements.Length > 0)
         for (var i = 0; i < myElements.Length; i++)
         {
           //myElements[i] = new MenuElements();
             myElements[i].SetParent(this);
             myElements[i].parentSize.x = size.x;
             myElements[i].parentSize.y = size.y;
             startPosition = position;
             myElements[i].percentage = true;
             myElements[i].Start();
         }
   }

 }


I have tried allot of things but i can`t put it to work, what am i doing wrong? My objective is to instantiate the class MenuElements inside MenuWindow, after inserting the number of instances of that class in the inspector.

Comment
Add comment
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

2 Replies

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

Answer by GuyTidhar · Feb 20, 2013 at 02:51 PM

Add: 'System.Serializable' attribute above your non monobehvaiour class

 [System.Serializable]
 public class MenuElements
 {
 //My public vars enums methods etc
  
 }
Comment
Add comment · Show 2 · 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 Bunny83 · Feb 20, 2013 at 02:53 PM 1
Share

:) Same second....

avatar image GuyTidhar · Feb 20, 2013 at 02:55 PM 0
Share

Indeed... (+ Character so I can post this. But now that's a bundle more characters. What a predicament!)

avatar image
2

Answer by Bunny83 · Feb 20, 2013 at 02:51 PM

In UnityScript every class you create gets by default the System.Serializable attribute. In C# you have to add it to your class so it's displayed in the inspector. When it's displayed in the inspector you don't need to create the array or the classes since that is done by the inspector.

 [System.Serializable]
 public class MenuElements
 {
 
 }


Comment
Add comment · 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

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Reflection on Unity class 1 Answer

C# NullRefException accessing static class, array value 1 Answer

Programmmed Animation (making transform move to vectors with code) 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