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 LMan · May 15, 2015 at 04:09 PM · c#arrayreferencepointershortcut

How would one use a pointer variable to reference an array element?

Suppose I have an array of a custom class that takes a string and two ints:

 public Statistic[] stats = new Statistic[] 
     {new Statistic("Health", 100, 100),
      new Statistic("Stamina", 50, 50), 
      new Statistic("Attack", 5, 5),
      new Statistic("Defend", 4, 4),
      new Statistic("Speed", 3, 3),
      new Statistic("Luck", 3, 3)};

I would love it if I could reference these elements through some kind of shorthand. For instance,

 //referencing
 
 stats.hp
 
 //would point to the same data as
 
 stats[0]

Is this a thing? My apologies if this has been asked before, I did some searching but I found it difficult to come up with a concise query. It's really a readability thing that would be really nice to have. Thanks awfully for the help! -Luke

Comment
Add comment · Show 2
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 DoTA_KAMIKADzE · May 15, 2015 at 04:29 PM 0
Share

I don't really get your problem. "stats[0].hp" should work if your Statistic class has public/internal variable/property named hp.

avatar image Owen-Reynolds · May 15, 2015 at 06:39 PM 0
Share

This is really a basic C# question. What I mean is, you can get a much better explanation in a regular C# guide.

All class-variables in C# are really just pointers, and any class-variable can not use new, and ins$$anonymous$$d just point to any existing item. A common example is how GameObject player=GameObject.Find("player"); uses player as a pointer (C# calls them references.)

You can't point to a item in any old array (like an array of ints.) Array is just a red-herring. You can point to any class, whether the class is in an array or not.

1 Reply

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

Answer by LRG · May 15, 2015 at 04:30 PM

Yes, quite easily:

 public Statistic[] stats = new Statistic[] 
      {new Statistic("Health", 100, 100),
       new Statistic("Stamina", 50, 50), 
       new Statistic("Attack", 5, 5),
       new Statistic("Defend", 4, 4),
       new Statistic("Speed", 3, 3),
       new Statistic("Luck", 3, 3)};
 
 
 var stat = stats[2];
 
 
 stat.hp = 200;
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 LMan · May 15, 2015 at 05:20 PM 0
Share

Well, that doesn't quite solve the problem, but it helped me figure it out so I'm marking it right.

In the first place, I neglected to tell you that stats is initialized outside of the game loop- like before Start or Update in the class.

so that means that the pointer variable has to be initialized inside start or awake.

stat.hp is a typo, I think you meant stats.hp- and that wouldn't work as hp is not a field of the class Statistic. That's what you got wrong.

var stat is javascript (not your fault, I didn't specify.), so that would be

 Statistic hp = stats[0];

and then I can change the value of hp and it will also change stats[0];

avatar image LRG · May 18, 2015 at 11:08 AM 0
Share

Glad it helped, to an extent!

stat.hp was not exactly a typo, but indeed, if hp is not an attribute of the Statistics class, it won't work. stats.hp would also fail but for a different reason; it wouldn't be trying to access the hp field within the Statistic class, but the hp field within a Statistic[] array. This is not possible because an array wouldn't have such a field (you could, however, access, for instance, stats.length).

var stat = stats[2] is both C# and JavaScript code, though the var keyword is probably indeed more popular in JavaScript. They have different meanings, however. In JavaScript var is used to declare a local variable. In C# var is used as a shortcut for the type, which is statically inferred. Basically, it's just syntactic sugar to type less. You can use it within a method scope but not within a class scope, so depending on your case you may not be able to use it anyway.

avatar image LMan · May 23, 2015 at 11:08 PM 0
Share

Thanks so much! I had no idea about var.

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

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

Related Questions

array is pointer and float variable is not - why not? C# 1 Answer

Pointer or Copied Value? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Store reference to array as variable 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