• 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
0
Question by rhbrr5hrfgdfgw · Apr 16, 2015 at 01:14 AM · c#arrayclass

Error passing class in method c#

I want to pass a variable with type class : "bpButtonID" in a method so the method will change the values of the variable. Main code:

 public static void check(bpButtonID id, int newID)
             {
                 id[] array = new id[8];
                 for (int i = 0; i < array.Length; ++i)
                 {
                     array[i] = new bpButtonID(5);
                     print(array[i].GetID());
                         array[i].SetID(newID);//Put the new id
                         print(array[i].GetID());
                 }
             }

and then somewhere in the update i will call. t$$anonymous$$s is just an example method so it doesnt really do somet$$anonymous$$ng.

bpButtonID class:

         private int id;
         public ID(int id)
         {
             t$$anonymous$$s.id = id;
         }
     public int GetID(){
         return t$$anonymous$$s.id;
     }
     public void SetID(int id){
         t$$anonymous$$s.id = id;    
     }
     }

And i get an error (probably because of t$$anonymous$$s line: "id[] array = new id[8];") because i dont know how to call it properly. The error:

 The type or namespace name `id' could not be found. Are you missing a using directive or an assembly reference?



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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by DoTA_KAMIKADzE · Apr 16, 2015 at 01:18 AM

So because you say that it is an example code I'll not dig into it then and just answer your question.

The error basically says that you can't use Instance of your class to instantiate array of your class. And yes you got it right. To create properly your new array of your Class you should use class not its instance, like that:

 bpButtonID[] array = new bpButtonID[8];

P.S. So in the comment below you say you have initialized class already and pass it to your check method like that:

 check(myInitializedArray, someInt);

But then what you want to do with your class? That isn't obvious from your code. Specify that.

For now I'll just try to guess once more - you just forgot to initialize your bpButtonID as array? Then just initialize it as an array and pass as an array and change the value of it, you don't need to create another array:

 //make array:
 bpButtonID[] mainArray;
 //pass array:
 check(mainArray, someInt);
 //then do somet$$anonymous$$ng with your array:
 public static void check(bpButtonID[] id, int newID)
 {
     for (int i = 0; i < id.Length; ++i)
     {
         id[i] = new bpButtonID(5);
         print(id[i].GetID());
         id[i].SetID(newID);
         print(id[i].GetID());
     }
 }

I'll make the second guess, you want to pass array and clone it, then:

 public static void check(bpButtonID[] id, int newID)
 {
     bpButtonID[] array = new bpButtonID[id.Length];
     Array.Copy(id, array, id.Length);
     for (int i = 0; i < array.Length; ++i)
     {
         array[i] = new bpButtonID(5);
         print(array[i].GetID());
         array[i].SetID(newID);//Put the new id
         print(array[i].GetID());
     }
 }

If in the end you'll find out that those^ are still not what you want then just state clearly what you want...

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 rhbrr5hrfgdfgw · Apr 16, 2015 at 09:48 AM 0
Share

I know i have to use it like that, but i want to be able to make a variable at the start of the class like this: bpButtonID mainArray;

and then in the method use the variable in the method to change its values. What you wrote going to make an array every time and i dont want it i want one array so i can get and set values from it

avatar image DoTA_KAMIKADzE · Apr 16, 2015 at 11:15 AM 0
Share

I've updated my answer, check the P.S. section.

avatar image
0

Answer by ndrummer31 · Apr 16, 2015 at 05:21 AM

Hi rhbrr5hrfgdfgw! That error is usually caused by having a leak in a class. Check to make sure you have all your curly brackets.

Also, instead of using two separate functions to get and set the id, why not just use a property?

 private int _id
 public int ID
 {
     get { return t$$anonymous$$s._id; }
     set { t$$anonymous$$s._id = value; }
 }

You may already be aware of t$$anonymous$$s and be doing that for a reason. In the case you aren't, I hope you learned somet$$anonymous$$ng.

I hoped t$$anonymous$$s helped! Happy coding!

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 rhbrr5hrfgdfgw · Apr 16, 2015 at 09:45 AM 0
Share

Since i am using unity 4.2 or something like that i dont think i can use property. But thank you.

avatar image
0

Answer by rhbrr5hrfgdfgw · Apr 16, 2015 at 11:03 AM

Well i kinda fixed it, so if anyone need to know:

Instead of calling the variable from the method parameters i did:

 public bpButtonID[] bpArray;

And then i changed the length in the inspector to what i was needed, and then in the method:

 bpArray[i] = new bpButtonID();

So now i can get the values from the arrays.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Instantiating objects from a class? (C#) 1 Answer

C# Array of Child Objects 1 Answer

Writing an class array to disk? 1 Answer

C# Adding to an Array 1 Answer

Multiple Cars not working 1 Answer


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