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 /
This question was closed Jun 24, 2014 at 02:05 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by MonKcz · Jun 23, 2014 at 12:54 AM · referencelinktower-defense

[SOLVED]Get variable of an object that creates different object with a script

Hey guys again,

I am really sorry to ask maybe stupid question but I am struggling with this for a long time. I've searched through forums and answers but cant find my exact problem.

I have got a tower(gameObject) with a script where I can drag and drop bullet prefab with a bullet script.

alt text

How can I get a reference to a variable from Tower.script to Bullet.script ? Tower.script instantiates bullets which fly to target and I want to tell every bullet what dmg it should take to the enemy.

Keep in mind that I want to create many towers and every tower is upgradeable so dmg will change based on every upgrade of the tower.

Static variable is not an option and GetComponent works if script are on the same gameobject or child object (bullets are independent gameobjects). FindObject method is slow and wont work coz of many levels of towers. The only moment they have something in common is when bullet gameobject with a script is dropped in tower.script .

Maybe I am missing something or is there different use from these options?

Tower.script

 // shoot next bullet?
         timeLeft -= Time.deltaTime;
         if (timeLeft <= 0.0f) {
             // find the closest target (if any)
             Unit target = findClosestTarget();
             if (target != null) {        
                 // is it close enough?
                 if (Vector3.Distance(transform.position, target.transform.position) <= range) {
                     // spawn bullet
                     GameObject g = (GameObject)Instantiate(bulletPrefab.gameObject, transform.position, Quaternion.identity);
                     // get access to bullet component
                     Bullet b = g.GetComponent<Bullet>();
                     
                     // set destination        
                     b.setDestination(target.transform);
                     
                     // reset time
                     timeLeft = fireRate;
                 }
             }
         }


Bullet.script

         // was target reached?
         if (transform.position.Equals(destination.position)) {
 
             Unit t = destination.GetComponent<Unit>();
 
             if (t != null) 
             {
 
             t.health = t.health - 1 (tower.dmg should be here);
             
             // unit elimination
             if (t.health <= 0)
                 t.onDeath();
             }
             
             // destroy bullet
             Destroy(gameObject);
         }

1.png (6.9 kB)
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

  • Sort: 
avatar image
0
Best Answer

Answer by Kiwasi · Jun 23, 2014 at 03:32 AM

I don't understand the question.

You already have a good reference to the bullet component at line 12. At line 13 you could write

 b.damage = dmg;

Then on your bullet script use

 public int damage;
 
 // Skip to line 9
 t.health = t.health - damage;
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 MonKcz · Jun 23, 2014 at 09:51 AM 0
Share

Lol I have to admit this was really stupid question. I was writing this at about one clock A$$anonymous$$ so I wasn't thinking properly. Thanks anyway, my bad, next time I will check more my code than forums and answers on unity3d. Really appreciated :) Once game is done I will all people who helped me list in credits :)

avatar image Kiwasi · Jun 24, 2014 at 01:31 AM 0
Share

All good. Glad I could help.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to run function in another script with prefabs? C# 2 Answers

the most RESOURCE EFFICIENT way of referencing 1 Answer

how can i make a player get in a vehicle? 1 Answer

audio between scripts 1 Answer

Turret A.I scripts 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