Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Change Variable Depending On Parent Object

Discussion in 'Scripting' started by DathranEU9, Nov 22, 2022.

  1. DathranEU9

    DathranEU9

    Joined:
    Apr 27, 2021
    Posts:
    10
    Hey,

    Not sure how to word this entirely, so I'll lay out my thought process below:

    I have two towers, we'll call them Tower A & Tower B.

    Tower A Damage = 5
    Tower B Damage = 10 (as this tower has been upgraded)

    How do I make it so the bullets that come from Tower B do 10 Damage, but the ones from Tower A stay as 5 Damage?

    The Towers are a separate script to the bullets that are instantiated. I've tried a lot of different ways but can't seem to achieve this without all bullets becoming one Damage value.

    Tower script name = Building
    Bullet script name = projectileArrow

    Tower Script to instantiate a bullet:
    Code (CSharp):
    1.    
    2.  
    3.     public GameObject projectileShoot;
    4.  
    5. void CheckIfTimeToFire()
    6.     {
    7.         if (Time.time > nextFire)
    8.         {
    9.                 Instantiate (projectileShoot, transform.position, Quaternion.identity);
    10.                 nextFire = Time.time + fireRate;
    11.         }
    12.     }
    Bullet script that references damage:
    Code (CSharp):
    1.        
    2.  
    3.  public int damage;
    4.  
    5. void OnTriggerEnter2D(Collider2D hitInfo)
    6.      {
    7.        
    8.         Enemy enemy = hitInfo.GetComponent<Enemy>();
    9.        
    10.         if (enemy != null)
    11.          {                    
    12.             enemy.TakeHit(damage);
    13.             Destroy(gameObject);
    14.          }
    15.        
    16.      }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,749
    So many ways... the most common is to just make two prefabs, one for each tower, sharing the same script, except you have a
    public int DamageDealt;
    field that you set to different values on each prefab.

    Another way might be to configure a common tower with something like a ScriptableObject.

    If you need more ways, hit any one of the 17,000,000 Youtube tutorials available for tower defense games.
     
    DathranEU9 likes this.
  3. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,509
    upload_2022-11-22_16-52-16.jpeg
    We can't guess what you tried and why it might not have worked. We can only answer in as much detail as you give us.

    But, in short:
    1. Have a "damage" variable on the tower script, which is readable (e.g. a public getter property).
    2. Have a "damage" variable on the projectile, which is writable (e.g. a public setter property).
    3. At fire time, the shooting script should read the variable from the tower and write it to the projectile.
     
    DathranEU9 likes this.
  4. DathranEU9

    DathranEU9

    Joined:
    Apr 27, 2021
    Posts:
    10
    Managed to figure it out by setting the projectile variable when it is being instantiated by the tower.