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 sabliao · Oct 03, 2011 at 08:15 PM · gameobjectinstanceaddcomponentattach

How to add new instance of a script to a game object?

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

 hpScript = Instantiate(HealthScaling);
 hpBar.AddComponent(hpScript);

Second try

hpScript = hpBar.AddComponent("HealthScaling") as HealthScaling;

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

2 Replies

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

Answer by sabliao · Oct 03, 2011 at 08:34 PM

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

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 aldonaletto · Oct 03, 2011 at 08:35 PM

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.
To avoid this problem you could remove the static declaration from the variable declaration - but since static variables are referenced as ScriptName.VariableName, all scripts accessing them should be modified to use that boring GetComponent method.

Comment
Add comment · Show 2 · 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 sabliao · Oct 03, 2011 at 08:54 PM 0
Share

haha, yeah, I figured it out a couple $$anonymous$$utes later, but thanks for responding anyway. (:

avatar image RackyMac · Aug 26, 2012 at 02:09 AM 0
Share

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 :)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

GO.AddComponent just adds an empty Script 1 Answer

Create an object and assign code to it via script? 1 Answer

Can I use AddComponent to add (this.script) to an object? 1 Answer

Add an instance of a script as a component? 1 Answer

How to add an existing script instance to a GameObject 2 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