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 blackmethod · Aug 14, 2011 at 11:16 PM · collisiondetectionvaluespasshealth

Transferring values from one script to another script?

I have looked around but haven't found anything that helped me quite yet, just for a test I made a scene with a camera that shot a cube at a wall. What I want to test is that after a couple hits the wall should disappear/break. Here's what I had on the bullet so far, I need a way to transfer the values from this script to my wall/playerproperties script.

Bulletcollision.js:

    var damage = 5;
 function OnCollisionEnter(theCollision : Collision){
  if(theCollision.gameObject.name == "Wall"){
  
 
 }



Playerproperties:

 var health=20;
 
 function Update()
 
 {
 
 if (health <= 0)
 destroy(gameObject);
 
 }

Assistance would be great, thanks ahead of time

  • Valestrom

Comment
Add comment · Show 1
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 Bovine · Aug 14, 2011 at 11:30 PM 0
Share

Hi There

It's not entirely clear to me what you are trying to do, but it strikes me that a script on your wall should track its own health and do the deduction/destruction. Please post more details!

H

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by HHameline · Aug 15, 2011 at 02:06 AM

very simple the scripts are attached to a game object so you would want to say something like

 //this line is in the playerproerties script
 public GameObject obj1; //drag the object with the bullet collision script on this

then you can say something like this:

 obj1.GetComponent<Bulletcollision>().<YOUR VARIABLE NAME HERE> = <VALUE>;

Unfortunately I'm not incredibly familiar with javascript so this is written in C# but you should be able to convert it fairly easily.

Hope that helps :D Hans

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 Joshua · Aug 15, 2011 at 02:31 AM 1
Share

For completeness, js would look like:

 obj : GameObject;
 obj1.GetComponent.< Bulletcollision >().<var name here> = <value here>;
avatar image HHameline · Aug 15, 2011 at 02:36 AM 0
Share

Thanks Joshua :)

avatar image
0

Answer by rimawi · Aug 14, 2011 at 11:42 PM

  var damage = 5;
 function OnCollisionEnter(theCollision : Collision){
  if(theCollision.gameObject.name == "Wall"){
 
 //take 5 points from health of the wall
 Player.health -= damage;
 
 }


Player.js /// Iam assuming the name of the file is Player static var health=20;

 function Update()
 
 {
 
 if (health <= 0)
 destroy(gameObject);
 
 }

// notice the word static before health variable you can update static var across files.

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 Joshua · Aug 15, 2011 at 02:32 AM 0
Share

This answer is technically correct, but when some one asks how to reference other scripts and ins$$anonymous$$d you tell him to use static variables - that's usually only confusing. And it won't work if he has several players.

avatar image Bovine · Aug 15, 2011 at 03:21 AM 0
Share

Well it doesn't answer the question title, but it does tackle the problem. It also scales better - I understand calling GetComponent() is best avoided - I'm personally a little paranoid about it so in this instance I might have something, perhaps a gun script, know about the player and squirrel a reference to the player into a bullet and then, contrary to my suggestion above, the bullet could identify it had hit the player and then do damage to it and avoid the GetComponent() call and any statics.

That said, In my own game when the player script is created in Awake() I store it in a static property - Player.Player so I can get at it from anywhere. Of course that only works well for single player but you could adapt the principal for multi-player I am sure... GetComponent() is also probably less of an issue on desktops - I'm dealing with iOS devices see!

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

7 People are following this question.

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

Related Questions

How to make player keep getting hurt when touching fire? 1 Answer

I want to pass objects through each other AND detect the collision 1 Answer

Collision detection not working properly with 2D sprites 1 Answer

For loop on collision (two hit kill) 2 Answers

Best practice for OnTriggerEnter detection 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