-1

I am new to installed Unity 5. I have some problem in the following code:

void OnTriggerEnter(Collider other) {
        var air = other.collider.gameObject.GetComponent<DamageManager>();
        if(air){
            air.HP += HPFill;   
        }
    }   
peterh
  • 11,875
  • 18
  • 85
  • 108
Zia Ur Rehman
  • 235
  • 5
  • 16
  • 1
    So whats the problem? Any errors in console? – Paweł Marecki Mar 09 '16 at 10:46
  • @PawełMarecki: Error: `UnityEngine.Component.collider' is obsolete: `Property collider has been deprecated. Use GetComponent() instead. (UnityUpgradable)' – Zia Ur Rehman Mar 09 '16 at 10:51
  • So why don't you use `GetComponent()` instead? – diiN__________ Mar 09 '16 at 10:53
  • Look at this `OnTriggerEnter(Collider other)` here you got `Collider` as parameter and next line you try to access `other.collider`. Anyway you should be able get componenet like this `var air = other.gameObject.GetComponent();`. I'm not able to test this but you can try for yourself. – Paweł Marecki Mar 09 '16 at 10:54

2 Answers2

2

Replace var air = other.collider.gameObject.GetComponent<DamageManager>() with var air = other.GetComponent<Collider>().gameObject.GetComponent<DamageManager>();

void OnTriggerEnter(Collider other)
    {
        var air = other.GetComponent<Collider>().gameObject.GetComponent<DamageManager>();

        if (air)
        {
            air.HP += HPFill;
        }
    }
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • other is already the Collider component and a Component has a GetComponent member method so no need to reach for the gameObject. – Everts Mar 09 '16 at 11:54
  • I know it is. I translated what he had to exactly what it would look like in Unity 5. other.GetComponent(); should do it but I provided the exact translation from what he provided. – Programmer Mar 09 '16 at 11:58
1
    var air = other.GetComponent<DamageManager>();

The collider in your code is actually redundant since other is already the collider component. It is like asking the reference to reach itself. And no need either to use the gameObject reference since Collider is a Component and then contains a GetComponent method.

The new versions of Unity are removing all component reference but transform and gameObject as they are always there.

Everts
  • 10,408
  • 2
  • 34
  • 45