• 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
4
Question by CrowdedWorlds · Sep 29, 2011 at 09:38 AM · c#childinttoggledeclaration

Check if it's the first time running the game?

Hi all!

Feel like I'm here way too often at the moment, but here's today's question!! :P I have a script that changes some in game variables at start based on when the game was LAST run. T$$anonymous$$s is all working fantastically with the one exception that if it is the first time the player is starting the game, it will insta kill them and trap them in that state no matter how many times the game has been rebooted.

My method for getting around t$$anonymous$$s was a first run check, an int that defaults to true but can be disabled by checking the playerprefs. If it's first time true, it skips the game state changes, and if it's false it runs through them all.

So my method of doing t$$anonymous$$s is as follows:

 void start ()
 {
 firstRun = PlayerPrefs.GetInt("savedFirstRun");
 if firstRun = 0
 {
 int firstRun = 1;
 }
 else
 Do lots of game save loading
 
 }

So the logic is, if it's the first run, change the variable in future to false for first run. If it's already false, go to game loading. However it spits out t$$anonymous$$s error

 Assets/DataMaster.cs(20,9): error CS0135: `firstRun' conflicts with a declaration in a c$$anonymous$$ld block

I've tried googling t$$anonymous$$s and w$$anonymous$$le I can take a guess what it means I don't truly understand the issue or have any idea how to fix it. If someone could clue me in or provide a solution it would be much appreciated

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 Kacer · Sep 29, 2011 at 10:28 AM 0
Share

When you're restarting the level, do you use application.loadlevel?

avatar image syclamoth · Sep 29, 2011 at 01:37 PM 0
Share

The problem is that you define firstRun twice! Give the second one a different name. I'm assuming what you posted isn't exact code- it's pretty nonsensical, but it gets the message across.

3 Replies

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

Answer by BerggreenDK · Sep 29, 2011 at 11:11 AM

Just place the variable OUTSIDE the functions/events, as a class variable

 int firstRun = 0; // variable inside the class, but not inside a function.
 
 void start ()
 {
   firstRun = PlayerPrefs.GetInt("savedFirstRun") ;
 
 
 if (firstRun == 0) // remember "==" for comparing, not "=" w$$anonymous$$ch assigns value
 {
   firstRun = 1;
 }
 else
 { 
    //Do lots of game save loading
 
 }

}

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 CrowdedWorlds · Sep 29, 2011 at 01:38 PM 1
Share

This solved it, thank you BerggreenDK :D == was the major killer here, thanks for the reminder. Operators seem to escape my brain way too often :P

avatar image alone1992 · Feb 03, 2014 at 06:09 PM 0
Share

thanks that is really nice notice

avatar image TX Manager · Mar 30, 2016 at 05:07 PM 0
Share

I've only one problem, how do I know how many times did the user opened the game?

avatar image
1

Answer by ShadowVipers · May 17, 2019 at 08:39 PM

Instead, you should do t$$anonymous$$s:

 if(PlayerPrefs.HasKey(<OneOfYourKeys>))
 {
        LoadDataMethod();
 }


w$$anonymous$$ch doesn't waste any space storing a 1 time use variable.

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
0

Answer by Flofoof · Mar 06, 2021 at 09:57 AM

There is a very simple way of asking unity if the player is running the first time, you can even count how many times the player ran the game!

just do t$$anonymous$$s:

 if(PlayerPrefs.GetString("unity.player_session_count") == "1"){
     //Ur stuff here!
 }

I used t$$anonymous$$s in my script:

 if(PlayerPrefs.GetString("unity.player_session_count") == "1")
     {
         if (balance == 0)
         {
             Console.Log("No balance found, adding 100$!");

             PlayerPrefs.SetInt("money", 100);
             balance = 100;
         }

         if (playerName == null || playerName == "")
         {
             Console.Log("No name found, opening register page!");
             registerPage.SetActive(true);
         }

     }

And it works perfectly fine!

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

11 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

Related Questions

Multiple Cars not working 1 Answer

"a" conflicts with a declaration in a child block 3 Answers

Distribute terrain in zones 3 Answers

Toggle tabs for a WoW like character sheet 0 Answers

How can I disable toggles when 2 toggles checked? 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