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 dragontorchslash · Aug 18, 2014 at 07:34 PM · c#classrpg

Question about classes C#

Ok, so I am trying to make a 2d rpg kinda akin to final fantasy with a better battle system. However, I am having trouble trying to figure out how to get my character classes between scenes. So I tried to make a static class with public variables to find out I cant do that. So here is what I need.

  1. I need a class to hold variables that all relate to one character

  2. I need to be able to change them from other scripts

  3. I need to be able to access them across different scenes

What would be the best way to go about doing this?

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
2
Best Answer

Answer by CHPedersen · Aug 18, 2014 at 07:48 PM

Your class should be a script you put on your player gameobject. In the Start method or Awake method of this script, you must call DontDestroyOnLoad which is exactly meant to preserve a gameobject including all its components through scene changes, thus accomplishing this.

Making everything static is an oft-chosen yet poor design choice for inter-scene persistence. Those variables that constitute defining characteristics about your player character should instead be made public properties, which are implemented like so, in C#:

 public int MyProperty { get; set; }

With an int for this example, though the type could be anything. The "get" and "set" parts afterwards ensure external code can read and set this variable. Properties like the int in this example are the standard way to accomplish these things in C#, but beware that they are not serialized in Unity, so they will not appear in the editor. It's possible to get them to do so, but if that is a requirement, the code must be slightly more involved, as you will have to declare the underlying variable exposed through the property yourself and make sure it is serialized. Such an implementation would look like this:

 public int MyProperty { get { return _myPropertysInt; } set { _myPropertysInt = value; } }

 [SerializeField]
 private int _myPropertysInt;



Comment
Add comment · Show 4 · 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 dragontorchslash · Aug 18, 2014 at 08:03 PM 0
Share

Thanks! Didn't know about the DontDestroyOnLoad. I will use that.

avatar image dragontorchslash · Aug 18, 2014 at 08:06 PM 0
Share

No I won't be using them in the editor. I only need to reference them in the code, sorry should have specified that. Basically all it is doing is holding my stats like hp, attack, etc. So I will get them from a save file, or define them if you start a new game.

avatar image CHPedersen · Aug 18, 2014 at 08:14 PM 0
Share

You're welcome. :) In this case, the auto-implemented property (the top one) will suffice. The only gotcha you have to beware of with DontDestroyOnLoad is that you don't revisit the scene that contains the original player gameobject without manually Destroy'ing the player. If you do, you'll get dublicate player objects, because the first one travelled along with you through all scene changes.

avatar image dragontorchslash · Aug 18, 2014 at 09:03 PM 0
Share

Thanks! That is nice to know because I am going to be carrying the stats over to battle, and after the battle going back to the first scene. So maybe it would be better to create them after the title screen, and by default none of my scenes have him, but since he isn't destroyed when loading he will carry over.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How do you make RPG class stat presets? 1 Answer

Creation of RPG Class Stat Presets? 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

C# List and GUI 3 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