|
I'm trying to have separate instances of a script called HealthScaling so that each player has its own health, and they're not all updating the same hp, which is my current problem. I thought the solution was for me to use the Instantiate(HealthScaling), set that to a variable name, and then add that variable name as a component. AddComponent() doesn't like that however. So I tried gameObject.AddComponent(HealthScaling), but then my script went a little wack when I test ran the game. How should I make a new instance of a script and attach it to a game object? *Variables have all been declared beforehand. First try Second try
(comments are locked)
|
|
Nevermind, I realized my problem was that, in an attempt to make it easier for another script to access the variable, I made hp static, which meant all instances of HealthScaling would share that variable. Now I just have to figure out how to pass that hp variable to my second script in another way...
(comments are locked)
|
|
When you instantiate a prefab, it comes with its own script instances. The same occurs when you add a script via AddComponent - its a whole new instance. Your problem seems to be another thing: probably the health variable is declared as static. Static variables are created only once, and no matter how many different instances of the script are running, all of them will read and write the same variable. haha, yeah, I figured it out a couple minutes later, but thanks for responding anyway. (:
Oct 03 '11 at 08:54 PM
sabliao
I just spent forever looking at why a variable wasn't updating and I had forgotten about it being static until i read this. Thanks :)
Aug 26 '12 at 02:09 AM
RackyMac
(comments are locked)
|
