Access variables/functions from another Component

前端 未结 3 630
攒了一身酷
攒了一身酷 2020-11-22 11:44

So im trying to change a variable in another script by touching a cube. Current setup

  • 1x Player
  • 1x Enemy

Each with their own scri

3条回答
  •  逝去的感伤
    2020-11-22 12:09

    The first thing to make this shorter (and eventually faster) would be to store this: gameObject.GetComponent() on Start() in a private variable (you should avoid calling GetComponent on a frequent basis if you can avoid it).

    For the Health variable, a way of avoiding GetComponent calls could be caching: you create a Dictionary and read from that as soon as this gameobject collided once

提交回复
热议问题