• 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 TeachMeSenpai · Jul 12, 2018 at 03:15 PM · boxcollider

Auto Saving using box collider?

I'm actually beginner here in unity. I'm creating a saving without using button, i want to use a trigger box collider so that when the player run to it, it will save but i can't find any tutorial like that but if you have tutorial guys can i get a link :).

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
0

Answer by Igor_Vasiak · Jul 12, 2018 at 04:13 PM

What you want is easily achievable. But, before anything else, I should encaurage you to search about Unity's JsonUtility and, well... Reading MSDN C# Guide, since it can and will help you with some C# core coding.


DISCLAIMER - Everything I wrote here works for me and correspond to my usual workflow, so opinions may differ on which is the best practice and which isn't.


Now, for me, the safest way to save stuff is to save to a .json file from an isolated serialized public class. Let me explain:

 // I have a Profile class. It holds my personal data.
 public class Profile
 {
     public string name;
     public string email;
     public string randomPasswordDecoderKey:

     public void Save ()
     {
         ProfileJSON profile = new ProfileJSON ()
         {
             name = this.name,
             email = this.email
         };

         // This string has the path to the file. You could save it to whererver you wanted to.
         // For simplicity's sake I'll use the StreamingAssets folder.
         string finalPath = $"{Application.streamingAssetsPath}/profile.json";

         // The newFile receives all of the values contained on the profile, converted to the
         // .json text format.
         // The .json file is then found through the path (if not, it is created) then opened.
         // The newFile is finally written into the .json file, and then the file is closed.
         string newFile = JsonUtility.ToJson(profile);
         File.WriteAllText(finalPath, newFile);
     }

     public void Load ()
     {
         ProfileJSON profile = new ProfileJSON ();

         // This string has the path to the file. You need to specify the same path as the save,
         // otherwise it won't be able to find the file.
         string finalPath = $"{Application.streamingAssetsPath}/profile.json";

         // The jsonString receives all of the values contained on the profile.json file.
         // Then we simply override the values of our ProfileJSON instance using FromJsonOverwrite.
         string jsonString = File.ReadAllText(finalPath);
         JsonUtility.FromJsonOverwrite(jsonString, profile);

         // Also, don't forget to set the values of this profile after that! :-)
         this.name = profile.name;
         this.email = profile.email;
     }
 }
 
 // This is a public Serializable class that is isolated from the rest of the code.
 // For some reason my Password Decoder Key is public, but I don't want to save it to a text file.
 // So, to avoid that we create this class, which only contains the name and the e-mail of the profile.
 //
 // Think of it like a temporary folder where you'll place your files before moving 'em
 // to the final directory.
 [System.Serializable]
 public class ProfileJSON
 {
     public string name;
     public string email;
 }

You could just call the Save method on OnTriggerEnter. The Load one could be used on either Start or Awake.


Hopefully this answer will help you in some way!


PS: Please, DON'T forget to add using System.IO at the top of the .cs file you're using to save stuff.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

87 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 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 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 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

Box Collider independence from object rotation 1 Answer

Collider Displacement Issue 0 Answers

Why my box collider object goes into another box collider object? 1 Answer

How to convert a BoxCollider2D bounds to world space? 2 Answers

Collider around GUI text 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