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 Major · Aug 10, 2012 at 12:22 AM · guigameobjectkilldead

Access Var From Another GameObject

i need to access a variable from another game object. what i wan't to happen is have a variable called kills go up by 1 every time the dead function is called by the script controlling the enemy prefab. here's my script(javascript).

Health script for enemy:

 var Health : int = 1;
 var Type1Damage : int = 1;
 var Type2Damage : int = 5;
 var Type3Damage : int = 10;
 var Blood : Transform;
 
 function Start () 
 {
 }
 
 function LateUpdate()
 {
  if(Health <= 0)
  {
  Health = 0;
  Dead();
  }
 }
 
 function OnCollisionEnter(collision : Collision)
 {
  if(collision.gameObject.tag == "Type 1")
  {
  Health -= Type1Damage;
  }
  
 if(collision.gameObject.tag == "Type 2")
  {
  Health -= Type2Damage;
  }
  
  if(collision.gameObject.tag == "Type 3")
  {
  Health -= Type3Damage;
  }
 }
 
 function Dead()
 { 
  Destroy(gameObject);
  Instantiate(Blood, transform.position, transform.rotation);
 }

GUI script for camera:

 public var Kills : int = 0;
 
 function Update () 
 {
 }
 function OnGUI()
 {
  GUI.Label(Rect(Screen.width / 2, 10, 60, 20), "" + Kills);
 }
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
0
Best Answer

Answer by Seth-Bergman · Aug 10, 2012 at 02:06 AM

make it static:

 static var Kills : int = 0;

then access it by the name of the script:

 function Dead()
 { 
 HealthScriptName.Kills++;
  Destroy(gameObject);
  Instantiate(Blood, transform.position, transform.rotation);
 }


otherwise, you could create a var to store the player script:

 var healthScript : HealthScript;
 
 function Start(){
 healthScript = GameObject.Find("Player").GetComponent(HealthScript);
 }

then you access it with that:

 healthScript.Kills++;
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 Major · Aug 10, 2012 at 05:35 PM 0
Share

i can't believe it was that simple. i fill like an idiot to think i had to use .getcomponent. :)

avatar image Eric5h5 · Aug 10, 2012 at 06:26 PM 0
Share

Be careful about using static; it means that there is only one instance of the variable. In most cases you should use GetComponent.

avatar image Major · Aug 10, 2012 at 06:50 PM 0
Share

okay got it! :)

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

9 People are following this question.

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

Related Questions

Button then instanitates gameobject 1 Answer

The GameObject contain the GUI 0 Answers

Gathering GameObjects and then creating a button for each GameObject?? 2 Answers

Need Ideas How To Get Started With A In Game Computer? 1 Answer

Can't destroy an arrray with GUI Text 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