1
I have a screen with two 2d objects: player
and enemy
, both are prefab
of an object called fighter
.
They have three properties: attack
, defense
and hp
.
I created a script and included in the camera, it is who will control the game.
I want to know how to make the camera script decrease the hp
of one of my objects.
Class fighter
:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fighter: MonoBehaviour {
public int attack;
public int defense;
public int hp;
public int mp;
// Use this for initialization
void Start () {
int range = Random.Range (150, 200);
attack = range;
range = Random.Range (100, 150);
defense = range;
range = Random.Range (800, 1000);
hp = range;
}
// Update is called once per frame
void Update () {
}
}
Class that’s on camera:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class turnBased : MonoBehaviour {
private BattleStates currentState;
// Use this for initialization
void Start () {
}
void Update() {
}
void OnGUI(){
if (GUILayout.Button ("ATTACK")) {
//crio um botão e ao ser clicado, deve causar dano ao inimigo
enemy.hp -= 10; //<<<<aqui deve vir o código
}
}
}
Colleague, your question is not at all clear (but it was not I who denied it, okay?). First, what is the name of the class these objects use? It is
fighter
really? If yes, you tried to doplayer.GetComponent<fighter>().hp = 2;
, for example? Put snippets of your code, otherwise no one will be able to help you.– Luiz Vieira
@Luizvieira changed. I hope you can help better now. hug.
– Italo Rodrigo