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 maggot · Dec 12, 2011 at 12:09 PM · c#variablesstructaccessing

Accessing variables in a structure in a class from another class with cSharp

I have a class called Bullet which contains a struct of bullet. How do I access variables contained in this structure, from another class using cSharp?

 public class Bullet : MonoBehaviour
 {
     struct bullet
     {   
             private float _shootForce;
             public float shootForce    // The force at which missiles travel 
             {
                 get{return _shootForce;}
                 set{_shootForce = value;}
             }
     }
 }
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
2
Best Answer

Answer by maggot · Dec 12, 2011 at 04:36 PM

It was a struct called Bullet before, but I wanted to put it into a class, so I named the class Bullet and the struct bullet. I got it working now - and renamed some code. What I was missing was an instance of struct bullet(old name) in the Bullet class.

  public class Bullet
  {
     public struct bulletData
     {
          private float _shootForce;
          public float shootForce    // The force at which missiles travel 
          {
              get{return _shootForce};
              set{_shootForce = value};
          }
     }
     public bulletData playerBullet;
 }

Then in another class I invoke the Bullet class with :

     Bullet B = new Bullet();

I can then access the bulletData structure by code like :

 B.playerBullet.shootForce = 2f; 

and

 float shootForce = B.playerBullet.shootForce;
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 · Dec 12, 2011 at 04:50 PM 0
Share

Shouldn't you have your "playerBullet" variable declared somewhere in your Bullet class? ;)

avatar image maggot · Dec 12, 2011 at 05:21 PM 0
Share

whoops! Added public bulletData playerBullet; to the public class Bullet code above

avatar image
1

Answer by jheiling · Dec 12, 2011 at 04:01 PM

You have a struct called bullet, but no instance of that struct. But what are you trying to achieve by having a class called "Bullet" containing a struct called "bullet"? That's just wrong!

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
avatar image
1

Answer by Bunny83 · Dec 12, 2011 at 04:27 PM

Like jheiling said: You don't have an instance of your struct, only a definition. A definition is just some kind of a "blueprint". It doesn't contain any data. You have to create an instance of that struct. It's the same for classes. A class definition just tells the compiler how an instance of this class looks like. In Unity by dragging a class onto a GameObject you create an instance of this class. If you want the class instance have an instance of your struct, you should create one and store it in a variable. Something like that:

 public class Bullet : MonoBehaviour
 {
     private struct bullet
     {   
         private float _shootForce;
         public float shootForce // The force at which missiles travel 
         {
            get { return _shootForce; }
            set { _shootForce = value; }
         }
     }
     private bullet _bulletData;
     private void Awake()
     {
          _bulletData = new bullet();
     }
     private void Update()
     {
         // do something with _bulletData
     }
 }

Some further notes:

  • Your struct is defined as private (private is the default visibility in C#) so you can't use the type outside of your class.

  • If you define your struct as public you can use the struct anywhere by the full type-path. This declares a variable with the type of your struct: private Bullet.bullet myVariable;

  • Type names and method names should ALWAYS start with a capital letter. Variable names should always start lower-case. Usually properties also starts with a capital letter but some people treat them like variables.

  • You should also think about why you want to seperate some data / functions in a seperate struct and find a proper name for it. A struct "bullet" in a class "Bullet" really makes no sense. If the struct is responsible for a specific aspect of your class, find a proper name.

Comment
Add comment · Show 3 · 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 maggot · Dec 12, 2011 at 04:41 PM 0
Share

Good points, I was editing my answer and so missed this one.

avatar image F-N · Feb 13, 2014 at 08:13 PM 0
Share

_bulletData is already used to acces the struct

avatar image Bunny83 · Feb 13, 2014 at 10:48 PM 0
Share

@fn:
Uhm, sorry i don't get your comment. _bulletData is used where? In my example or his question? Or in his answer? It would be great if you could clarify your statement a bit since this question is solved for two years now ;)

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

6 People are following this question.

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

Related Questions

Adding variables from all scripts 2 Answers

Invoke on Struct or some else time wait 1 Answer

Error Accessing Other Script's Variables 1 Answer

How do I change a variable in Script A from inside Script B 1 Answer

Accessing Other Script Variables (C#) 2 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