Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Kyouri · Oct 05, 2010 at 06:38 PM · getcomponentvariablesname

How to access scripts other than by name.

I'm very new to Unity, and for the most part game creation. Even so, this is such a simple issue I feel kind of dumb for not being able to solve it haha.

I have a stats script attached to each of my game objects that keeps track of things like health, mass, damage, etc. It also holds special functions that are for each instance of that object. Then I have a weapon, say a lazer gun. That lazer needs to access the health variable in that stats script. The problem is, that stats script is a different script in every single object I have. For instance, I have PlayerShipStats, or JunkBlockStats, etc.

I can't use getComponent<PlayerShipStats> because the lazer also needs to be able to access getComponent<JunkBlockStats> as well. Or, any other number of objects.

Is there an easy way to do this? If not, should I just make one really long script called "Stats" and condense them all? It seems terribly unorganized... Thank You for the help, or just reading at all really.

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
2
Best Answer

Answer by Molix · Oct 05, 2010 at 06:56 PM

A few ways come to mind. You could create a stats class or interface which your individual items inherit from or implement, or you could messages.

(the following is off-the-cuff code for illustration)

e.g. inherit from base class

class StatItem : MonoBehaviour
{
  public abstract void AddHealth( int delta );
}
class PlayerShipStats : StatItem
{
  int health = 10;
  public override void AddHealth( int delta ) { health += delta; }
}

And to use it:

StatItem stat = target.GetComponent<StatItem>();
stat.AddHealth( -10 );

e.g. implement interface

interface IHealth
{
  public void AddHealth( int delta );
}
public PlayerShipStats : MonoBehaviour, IHealth
{
  public void AddHealth( int delta ) { health += delta; }
}

And to use it:

IHealth stat = target.GetComponent<IHealth>();
stat.AddHealth( -10 );

e.g. SendMessage

void AddHealth( int delta ) { health += delta; }

And to use it:

target.SendMessage( "AddHealth", -10 );
Comment
Add comment · Show 3 · 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 Kyouri · Oct 05, 2010 at 07:22 PM 0
Share

Thanks a lot. To be honest, this level of syntax was a bit over my head. I got a friend to help me understand abstracting though, and thanks to you I have that down pat now. Interfaces though are a bit beyond me, and I'm just now learning of Send$$anonymous$$essage. Going to go through the documentation on Send$$anonymous$$essage right now and learn how to use it. Your answer was very helpful, even if I don't figure the other two out so easily you've fixed my problem and taught me a new important concept. Thanks a bunch!

avatar image Molix · Oct 05, 2010 at 08:01 PM 0
Share

I'm glad it helped. The Send$$anonymous$$essage way is probably the easiest; another nice thing about it is that any other components can just implement an "AddHealth" function too, and they'll all get called. So for example, an effect script could create some particles or play a sound when the health changes. Anyway, good luck!

avatar image Kyouri · Oct 05, 2010 at 08:38 PM 0
Share

Figured out how the other two methods posted work after some research. I learned a lot today! =P Using the Send$$anonymous$$essage method it now works much better than I ever could have hoped. $$anonymous$$ission Complete! <3

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

How to address scripts without knowing their names? 2 Answers

acces variables from other script 2 Answers

Accessing other gameobject's script variables : why doesn't this work? 2 Answers

When exactly do scripts have to be named exactly the same as the class they contain? 3 Answers

NullReferenceException when using GetComponent to access script variables 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