x


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);
    }
}

}

more ▼

asked Apr 21 '10 at 03:08 PM

Albert gravatar image

Albert
323 18 19 26

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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").

more ▼

answered May 05 '10 at 08:23 PM

burnumd gravatar image

burnumd
3.3k 22 34 71

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jul 12 '10 at 07:12 AM

Stanislav Pugach gravatar image

Stanislav Pugach
103 5 5 10

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Apr 21 '10 at 04:14 PM

andeeee gravatar image

andeeee ♦
1.4k 3 6 18

still freezes. . hmmm. .

Apr 21 '10 at 05:09 PM Albert
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3717
x3316
x177

asked: Apr 21 '10 at 03:08 PM

Seen: 1175 times

Last Updated: Apr 21 '10 at 03:08 PM