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
0
Question by myjean17 · Jan 29, 2012 at 06:09 PM · c#struct

Structs in C# Question

I am trying to use structs in unity for an inventory system. The struct has all of the variables that will be used for various types of information. I am trying to create a class that copies the struct in order to create a database for each item, so that other scripts can access this database to obtain information for each item. (if there is actually better logic for this please help me out, i have only just started doing anything like an inventory) here's my code. Please bear with my. I've just started structs:

The Struct:

 using System;
 
 public struct ItemObject {
         
     private string itemName;
     private string itemDescription;
     private int itemID;
     private string weaponType;
     private int itemDamage;
     private int itemAccuracy;
     private int itemSpeed;
     
     public ItemObject () {
         
         itemName = "name";
         itemDescription = "description";
         itemID = 0;
         weaponType = "melee";
         itemDamage = 2;
         itemAccuracy = 4;
         itemSpeed = 2;
         
     }
     
     public string Name {
         
         get { return itemName; }
         
         set { itemName = value; }
         
     }
     
     public string Description {
         
         get { return itemDescription; }
         
         set { itemDescription = value; }
         
     }
     
     public int ID {
     
         get { return itemID; }
         set { itemID = value; }
         
     }
     
     public string Type {
     
         get { return weaponType; }
         set { weaponType = value; }
         
     }
     
     public int Damage {
     
         get { return itemDamage; }
         set { itemDamage = value; }
         
     }
     
     public int Accuracy {
     
         get { return itemAccuracy; }
         set { itemAccuracy = value; }
         
     }
     
     public int Speed {
     
         get { return itemSpeed; }
         set { itemSpeed = value; }
         
     }
         
 }

And the "database": (barely anything, my problem is that when i try and do this unity says "Unexpected symbol "='"...

 using System;
 
 public class ItemDatabase {
     
     ItemObject item0;
     
     item0.Name = "name";
 
 }


And also, needless to say and not to be rude, id prefer not to be treated like a noob. I have a clue to what im doing. I understand a lot of people on these forums see this kind of question and respond in a way such as "well, i think your too inexperienced and should start with something a little simpler." I am inexperienced in this subject of coding. But I prefer a sophisticated response. Ill know what your talking about.

Thank you in advance for any help!

-myjean17

Comment
Add comment · Show 3
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 swisscoder · Jan 29, 2012 at 06:53 PM 1
Share

Why you use a struct ins$$anonymous$$d of a class?

avatar image rabbitfang · Jan 29, 2012 at 07:02 PM 0
Share

A stuct is very helpful when you need something that cannot have a null value, or where passing by value is preferable. I believe that they are also not processed through the normal garbage collector system because they are not objects but rather similar to ints and booleans. Not being actual objects make them ideal for when you need to create lots of short lived instances.

avatar image swisscoder · Jan 29, 2012 at 07:33 PM 0
Share

I not treat him as a noob. I asked the question about the struct to him. Because to use a class could be the better method, depending on the scenario. If he want to seriliaze the objects, it might be easier with structs. But propably he could use a class ins$$anonymous$$d of the struct and include the save/restore methods within that class.

3 Replies

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

Answer by swisscoder · Jan 29, 2012 at 06:54 PM

you can only assign the item0.Name within the constructor or a method of the class!

to better explain what I mean:

 using System;

 public class ItemDatabase {

     ItemObject item0;

     item0.Name = "name"; //not possible!

     public ItemDatabase(){
         item0.Name = "name"; //possible! (inside ctor)
     }

     public void init()
     {
        item0.Name = "name"; //possible! (inside method)
     }
 }
Comment
Add comment · Show 5 · 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 rabbitfang · Jan 29, 2012 at 06:59 PM 0
Share

um.... ItemObject.Name is a C# property (it can be used as a normal variable but it acts as getter/setter methods).

So, you can't really 'assign' item0.Name.

Also, this is kind of what he meant when he said that he does not want to be treated like a noob. Your answer is not all that sophisticated nor does it seem correct (from what I understand).

avatar image myjean17 · Jan 29, 2012 at 07:24 PM 0
Share

Ah.. feeling like a noob. I've been messing around with the code and forgot to change it to 'item1.Name' before I posted it on the forums. Edited my post, but anyways, I used the microsoft C# reference to accomplish some of this, so obviously it's implemented a bit differently in unity.

avatar image swisscoder · Jan 29, 2012 at 07:30 PM 1
Share

WTF! I help and write why it doesn't work. I made a exclamation mark, because it's the actual reason that myjean17 still is getting the error! In no line I offended myjean17!

@rabbitfang: even you can not assign a property of a localy defined variable outside a method or the constructor!

avatar image DanMarionette · Jan 29, 2012 at 07:43 PM 2
Share

I agree with swisscoder. This corrects the problem with the code in the question.

avatar image myjean17 · Jan 29, 2012 at 08:08 PM 1
Share

Ah yes this corrects the problem. Thanks for the help swisscoder!

avatar image
2

Answer by rabbitfang · Jan 29, 2012 at 06:38 PM

It would seem that item0.itemName is a private member. Try using item0.Name instead.

Also, when you ask noobish questions, you are then treated like a noob. However, I would not call this a noobish question, especially because you seem to understand the concept of constructors and C# properties. This is just a simple mistake that can happen often, much like a simple spelling error in code happens more often than people would like to admit.

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 myjean17 · Jan 29, 2012 at 07:28 PM 0
Share

$$anonymous$$y mistake- i did mean to change it back to 'item0.Name' before I posted it. Now, when i do that, everything seems to work except for that one line, which causes the error i mentioned in my above post, 'Unexpected symbol '=''.

avatar image
0

Answer by IfItsPeeLetItBe · Jun 11, 2014 at 08:53 PM

You can't make an assignment like this inside of a class.

 using System;
  
 public class ItemDatabase {
  
 ItemObject item0;
  
 item0.Name = "name";
  
 }

You need to place it inside of a Method like this.

 using System;
  
 public class ItemDatabase {
  
 ItemObject item0;

 void MethodForAssigningStuff()
 {
 item0.Name = "name";
 } 
 }

Then you will see that the issue goes away. I've been coding for about 9 months but stuff like this slips my mind all the time... Let my know if this helps... Or if something else goes wrong :|

Also struct does not allow for a constructor with no parameters.

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

8 People are following this question.

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

Related Questions

What is frame, How OnGuI is called every frame? 2 Answers

c# Adjust In-Game audio 1 Answer

Randomly Generated Objects 1 Answer

How may I get the children (direct and dependents) of a game object? 2 Answers

How to check if an object is colliding with another from another script ? 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