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 Doneyes · Mar 31, 2013 at 11:10 PM · collidervariableglobal

How to access this variable?

In this part of my script, there is a variable called colliders. A ray is cast to find the colliders and it finds all of them in a certain radius.

Some colliders have different scripts in them, but each of those different scripts share a variable called SystemHealth. How can I access that variable even though some colliders don't have any scripts and some of the scripts have different names?

Another way to word the question: How can I find the variable SystemHealth inside of the colliders. Here is the important part of the script.

 function Explode() {
  
     var colliders : Collider[] = Physics.OverlapSphere (transform.position, radius);
         for (var collider : Collider in colliders) {
                 var hit : RaycastHit;
                 if (Physics.Linecast(transform.position, collider.transform.position, hit)) {
                     if (hit.collider == collider) {
                         if (hit.rigidbody)
                             hit.rigidbody.AddExplosionForce(power - (power / hit.distance), transform.position, radius, 3.0);
                 }
             }
         }
 }
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

Answer by KMKxJOEY1 · Mar 31, 2013 at 11:21 PM

 hit.collider.gameObject.GetComponent<SystemHealth>().fuction name here();

In js I think it's

 hit.collider.gameObject.GetComponent(SystemHealth).function name here();
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 Doneyes · Mar 31, 2013 at 11:43 PM 0
Share

Note that the script name is not SystemHealth. It's just a variable that may or may not be inside the colliders. I'll try it out but I think the javascript is wrong. $$anonymous$$aybe I know how to fix it.

Edit: Yeah I added in:

 print(hit.collider.gameObject.GetComponent(SystemHealth));

and it calls SystemHealth an unknown identifier.

avatar image KMKxJOEY1 · Apr 01, 2013 at 02:24 PM 0
Share

oh lol then replace systemHealth with the script name

  var health = hit.collider.gameObject.GetComponent(script

name).SystemHealth;

avatar image
0

Answer by whydoidoit · Mar 31, 2013 at 11:50 PM

They don't share the the variable - just because they have the same name - it's not the same variable. If you want to share a variable you need to have a script with the shared variable that the other scripts access using GetComponent

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 Doneyes · Mar 31, 2013 at 11:59 PM 0
Share

I know they don't share the value, but they share the name. None the less, how can I access it in colliders? I tried to do:

 print(hit.collider.gameObject.GetComponent(SystemHealth));

but that told me that SystemHealth is an unknown identifier.

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

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Variable Not Changing. 0 Answers

Should I create local copy of global variable? 3 Answers

How to make a GLOBAL variable in javascript (Unity) 0 Answers

Problems with the triggers(not recognized) 1 Answer

Variable only changes for one object that has the script attached and not the other objects with the same script. 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