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 landon912 · Mar 09, 2013 at 01:26 AM · cloneitems

MemberWiseClone Protected?!?

Hello guys, Ive been trying to get this code to work for a few hours and I am really stumped. I get the error:

Assets/Scripts/ItemHolder.cs(122,36): error CS1540: Cannot access protected member object.MemberwiseClone()' via a qualifier of type ClassHolder.Item'. The qualifier must be of type `ItemHolder' or derived from it

ItemHolder is just a big database of items set up like :

 item[0].name = "Coin";            item[0].maxStack = 10000;    item[0].type = "Currency";    item[0].ID = 0; item[0].stats.baseValue = 1;

The error lines are:

 void ReturnItem(int id)
     {
     ClassHolder.Item passon = item[id].MemberwiseClone(); ///Error Line
     return passon;
     }

Im assuming the issue is that i dont have access to MemberWiseClone; Which I am guessing is a system class. What could be a solution is this issue.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Julien-Lynge · Mar 09, 2013 at 01:33 AM

So as I understand it from the documentation (http://msdn.microsoft.com/en-us/library/system.object.memberwiseclone.aspx) and your description, MemberwiseClone is a protected class, which means that it's private to a class and all its children. That means that it is not a public class and cannot be called from outside the class.

That's precisely what you're trying to do here - call it on a ClassHolder.Item from inside another class (ItemHolder presumably). If you want to be able to do a shallow clone using this method, you have to add a public method or property to ClassHolder.Item and then have that method or property do the clone. Example:

 public WhateverClass Clone()
 {
   return (WhateverClass)this.MemberwiseClone();
 }
Comment
Add comment · Show 10 · 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 landon912 · Mar 09, 2013 at 01:53 AM 0
Share

I moved the code to the ClassHolder, but i still get the same error. Am i missing something?

avatar image Julien-Lynge · Mar 14, 2013 at 11:07 PM 1
Share

I'm not sure what your code structure is, but if Item is a class you would have to move it there - basically, you have to move it to the class that defines whatever item[] is an array of.

avatar image Julien-Lynge · Mar 15, 2013 at 03:47 AM 1
Share

Looking at your error message it appears you have a type mismatch going on. Am I right in assu$$anonymous$$g that the item[] array is a ItemHolder, and you're trying to assign it to a ClassHolder.Item? Have you defined an explicit or implicit conversion between ItemHolder and ClassHolder.Item?

Perhaps try:

 ItemHolder temp = item[id].$$anonymous$$emberwiseClone();
 ClassHolder.Item passon = (ClassHolder.Item)temp;

if you've defined an explicit conversion.

At this point you may need to include some more code. At the very least I need to know what type that array is, and it might be helful just to give me the definitions for those two classes.

avatar image Julien-Lynge · Mar 15, 2013 at 07:41 PM 1
Share

Thanks, that's helpful.

Next question: Does ClassHolder.Item derive from ItemHolder, or ItemHolder derive from ClassHolder.Item? Why don't you just upload the entire script as an attachment so I can look throught it? Otherwise we might go around in circles for a while.

avatar image Julien-Lynge · Mar 15, 2013 at 07:51 PM 1
Share

Sounds good. In the meantime I suggest you read up on the error you're seeing (the CSXXXX is an error code, and you can always google them):

http://msdn.microsoft.com/en-us/library/s9zta243.aspx

All I'm going to be doing is trying to see if your code does what's in the example there.

Show more comments

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

11 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

Related Questions

Deleting an instance 2 Answers

How do I properly duplicate an object in a editor script? 3 Answers

I dont want "(clone)" but how to change? 2 Answers

Unity object cloning and public/private variables 0 Answers

save/load cloned game object 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