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 Rxanadu · Dec 17, 2013 at 03:47 AM · projectiledamageupgradeturret

Issues with one object receiving information from one of many other objects

I'm having an issue connecting a projectile to a turret in my game.

I am currently using a one-to-one relationship model between a projectile and the turret it came from. The turret send information (e.g. damage to enemies) to the projectile, and the projectile receives it. However, I'm having trouble figuring out how to get the projectile to actually receive the information from the turret.

The main problem lies within how the turrets behave in my game. Each turret has an upgrade system included which increases the amount of damage a projectile inflicts on a target. Each turret upgrades independently, however; given the one-to-one relationship mentioned earlier, this means that turret A could fire a projectile that inflicts more damage than turret B if A has upgraded more than B.

I currently have a script attached to the projectiles which I initially thought would receive information using a reference to a script attached to each turret in the game. However, the projectile's scripts has no way of differentiating which turret it came from.

If anyone has any idea as to help, please let me know. Thank you for taking the time out to read this.

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

Answer by robertbu · Dec 17, 2013 at 06:12 AM

The easiest way would be to have the turret set the parameters of the projectile as it is launched. The code might look like:

 var go = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
 var projScript = go.GetComponent(ProjScript);
 var projScript.damageFactor = damageFactor;
 var projScript.maxSpeed = maxSpeed;

Where 'damageFactor' and 'maxSpeed' are changed/upgraded by the turret.

Another solutions is to have a unique tag associated with each turret that is assigned to the projectiles. Then you can use GameObject.FindGameObjectsWithTag() to get a list of all the projectiles fired by one turret.

And another solution to have a 'fromTower' variable on the projectile script that uniquely identifies what tower each projectile comes from. You would first get a list of all projectiles using FindGameObjectsWithTag(), then you could cycle through them and change the values of any with a specific tower tag.

And there are many other ways to go about your task. Note many games implement a object cache so they can reuse things like projectiles rather than use expensive Instantiate() and Destroy() calls. If you do this, then you can cycle through the entries in your cache.

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 Rxanadu · Dec 17, 2013 at 03:56 PM 0
Share

I'm still having trouble internalizing your solution, so I've created a general scenario to help me out: each turret has a cache of n projectiles. Once one projectile from that cache is fired, it is given a tag "FiredProjectile." Projectile P, upon hitting its target, has to run a function which gives turret T, the turret P is fired from, experience points. This function, however, is located on a script attached to T that P is fired from.

How do I get P to run a function connected to T?

avatar image robertbu · Dec 17, 2013 at 04:14 PM 0
Share

This is yet a different solution. Since I don't have your code, let's assume that the script attached to the turret is called 'Turret' and the one on the projectile called 'Projectile':

So at the top of your projectile script, you will have:

 var turret : Turret;

Then when you launch the projectile:

 var go = Instantiate(projectile, transform.position, transform.rotation) as GameObject;
 var projectile = go.GetComponent(Projectile);
 projectile.turret = this;

Note I'm assu$$anonymous$$g here that the turret is the one launching the projectile. If it is not, then you will need to use GetComponent() to initialize 'projectile.turret'.

When the projectile hits, you can then use the 'turret' variable to access anything public in the turret script that launched the projectile. This includes functions.

Note this is not my original suggestion, but it may be what you need. $$anonymous$$y suggestion was have the turret set the experience points in the projectile as it was fired.

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

17 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Turret stops firing when enemy is out of range 1 Answer

Possibility of adding More than One Projectile 1 Answer

Fire turret "projectile" from origin to end 0 Answers

Damaging Enemy strangely not working. 0 Answers

Instantiate a projectile on key press "F" key / OnMouseDown! Help!!! 2 Answers


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