私はゲームをしていて、現在は近接攻撃を受けています。私は敵を見つけるなどのすべてのコードを実行しましたが、今はその敵にダメージを与える必要があります。それで私の敵(Slime)curHealth intのスクリプトにアクセスする必要があるのです。ここでc#別のクラスからのアクセス方法
は近接武器のためのコードである:ここで
{
private float meeleAttackStart = 0f;
private float meeleAttackCooldown = 0.5f;
public int meeleDamage = 40;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse0) && Time.time > meeleAttackStart + meeleAttackCooldown)
{
RaycastHit2D[] hitArea = Physics2D.BoxCastAll(transform.position, Vector2.one, 0, Vector2.up);
if(hitArea != null)
{
for(int i = 0; i < hitArea.Length; i = i+1)
{
if(hitArea[i].collider.tag == "Enemy")
{
// do stuff
}
}
}
meeleAttackStart = Time.time;
}
}
...
}
(いくつかのスウェーデン語の単語はそれについて心をいけないかもしれません)私の敵のために私のコードです(まだ進行中)
{
public int maxSlimeHealth = 40;
public int curSlimeHealth = 40;
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
をあなたがヒットする敵を発見した場合、それは 'instanceOfThatEnemy.curSlimeHealthでなければなりません;' しかし、あなたはあなたがそれを見つけた部分を見せなかったので、私は確かに言うことができません。 –
あなたがすでに試したことを示すことができればいいでしょう。 [尋ねる]を見てください。あなたは研究努力を示すべきです。あなたは[ColliderのAPI](https://docs.unity3d.com/ScriptReference/Collider.html)を見ましたか? ['gameObject'変数](https://docs.unity3d.com/ScriptReference/GameObject.html)を持っているのが分かりますが、これを何か別の方法で使ってみましたか?また、あなたの質問には本当の*質問*が含まれていません、私はどこにでも疑問符が表示されません。 – PJvG