Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 DrishtiS · Jul 27, 2017 at 05:39 AM · errormonobehaviour

[HELP]GetString is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead

I want to use the name1 variable in all my scripts, so i stored it using PlayerPrefs.SetString. It gives the following error when i use it in one of my javascript:

GetString is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead

I tried to use it in start() too but the problem remains the same. Well, I am not sure if the error is because of this but I have all my scripts in C# but one in javascript as it doesn't give any such error in my C# script. So, can I access the variable using PlayerPrefs.GetString in javascript when i have set it in C#? Please help me solve the error.

here is the link for my code. https://gist.github.com/anonymous/d03e6c18729f5e44680ee271e1b70dc1/revisions

I am a new to unity and to both C# and javascript and I am learning it on my own. So, that causes the trouble and that's the reason for the switch between the two languages. Sorry for being so silly.

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 Vicarian · Jul 27, 2017 at 04:38 PM

I've converted your script to C#. You do not need to learn both JavaScript and C# to work with Unity, and C# scripts are far easier to use.

 using System.IO;
 using UnityEngine.UI;

 public class Timer : MonoBehaviour {
     static int numberOfCheckPointsCrossed = 0;
     private string pathOfFile;
     private string fakeName;
     static float countDown = 240.0f;
 
     // This hasn't been assigned. Without knowing anything about your Hierarchy, 
     // I don't know how to assign it.
     Text counter;
     
     void Start() {
         fakeName   = PlayerPrefs.GetString ("name1");
         pathOfFile = "F:/Drishti/Data/Phase2/" + fakeName + ".txt";
     }
     
     void OnTriggerEnter(Collider col) {
         numberOfCheckPointsCrossed++;
         Destroy(gameObject);
     }
     
     void Update() {
         countDown -= Time.deltaTime;
         count = Mathf.RoundToInt (countDown);
         counter.text = "" + count.ToString ();
         
         if (countDown <= 0 || gameObject.tag == "Ca")
         {
             Application.LoadLevel("Up view");
             StreamWriter sw = new StreamWriter(pathOfFile);
             sw.WriteLine ("number of checkpoints crossed: " + numberOfCheckPointsCrossed);
             sw.flush();
             sw.Close();
         }
     } 
 }

Comment if your error isn't resolved by this port.

Comment
Add comment · Show 1 · 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 DrishtiS · Jul 31, 2017 at 09:32 AM 0
Share

Thank you for converting my Script to C#. the error disappeared once I restarted the Unity. But there is one more problem in my code,as in the above code, when I append my file in Update function

  if (countDown <= 0 || gameObject.tag == "Ca")
          {
              StreamWriter sw = new StreamWriter(pathOfFile, append:true);
              sw.WriteLine ("number of checkpoints crossed: " + numberOfCheckPointsCrossed);

} It would append the same value multiple times in the file.

![alt text][1]

screenshot-6.png (19.6 kB)

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

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

No Monobehaviour scripts in files 1 Answer

I can't add my script to a boss 2 Answers

unity script error 1 Answer

Problem with attaching scripts to objects 1 Answer

No MonoBehaviour scripts in the file(Unity 2019.2) 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