Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Albert · Apr 21, 2010 at 03:08 PM · freeze

Unity Freeze When Applying my Script to GameObject

Unity freezes when im applying this script to a Gameobject. I can't see whats wrong with my script, i did a lot of changes, but i can't really see what's wrong. my class name and the script's name is the same.

class playerStats extends MonoBehaviour

{

var Maximum : int; var Current : int;

var health = playerStats();

function deduct(x : playerStats) { x.Current = x.Current - x.Maximum; }

function Awake () { health.Maximum = 100; health.Current = 20; }

function Update () { if (Input.GetButtonDown("Fire1")) { deduct(health); Debug.Log(health.Current); } }

}

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

3 Replies

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

Answer by burnumd · May 05, 2010 at 08:23 PM

You can't call the constructor (as you do in the sixth line of your code, "var health = playerStats ()") of a MonoBehaviour. MonoBehaviours are, in essence, constructed when you add them as a component to something (That's why they have "Awake" and "Start"). Try taking out that assignment and you won't have any problem. However, I'm guessing then that your script won't function as you intend. It looks like you're trying to create a singleton. You might take a look at the wiki article on that topic. It's in C#, but the process for creating a single instance of some MonoBehaviour should be evident.

As a side note, classes are typically defined starting with a capital letter (ie, "class PlayerStats") to avoid confusion with variables, which should be named starting with a lowercase letter (ie, "var maximum").

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 andeeee · Apr 21, 2010 at 04:14 PM

You should initialise the health variable in the Awake function rather than the variable declaration.

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 Albert · Apr 21, 2010 at 05:09 PM 0
Share

still freezes. . hmmm. .

avatar image
0

Answer by Stanislav Pugach · Jul 12, 2010 at 07:12 AM

What happens is an instance of your playerStats class gets created when you add it to an object. In the process of creation, it needs to initialize all of the fields. Including the var health = playerStats(); initializer. This initializer creates a new object of class playerStats, which in turn need to initialize. This second object also executes the var health = playerStats(); line during its creation. That creates yet another object of the same type... etc etc

In effect, you created an endless cycle.

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.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Unity freezes on play 2 Answers

A node in a childnode? 1 Answer

Helpppp - AnimationState.layer ?????? -1 Answers

Player movement issue... 1 Answer

How To Add A FX When I Teleport. 0 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