Trying to make a gun, but it wont work? Please help.
I have been working on a game for some time now and I'm trying to make a FPS. My problem is that it doesn't do damage to the robot? Please help. Ill send the RobotScript, and the Shooting Script.
Shooting Script
using UnityEngine; using System.Collections; using UnityEngine.Networking;
public class Shooting : NetworkBehaviour {
public RobotScript RobotScript;
public int minWeoponDamage = 25;
public int maxWeoponDamage = 50;
public int weoponRange = 100;
public bool debugMode = true;
public Camera FPSCamera;
private void Update()
{
Ray ray = FPSCamera.ScreenPointToRay (new Vector2 (Screen.width / 2, Screen.height / 2));
RaycastHit hitInfo;
if (debugMode == true)
{
Debug.DrawRay(ray.origin, ray.direction * weoponRange, Color.green);
}
if(Input.GetKeyDown(KeyCode.Mouse0))
{
if(Physics.Raycast(ray, out hitInfo, weoponRange))
{
if(hitInfo.collider.tag == "Enemy"){
Debug.Log ("Enemy Hit");
RobotScript.instance.Health = 10;
}
}
}
}
}
Robot Script
using UnityEngine; using System.Collections;
public class RobotScript : MonoBehaviour {
public static RobotScript instance;
public int Health = 100;
static int MaxHealth = 100;
public static int DeathHealth = 1;
void Start () {
instance = this;
}
void Update () {
if (Health < DeathHealth) {
Destroy (this);
Debug.Log("Enemy should be dead");
}
}
}
Extra Info
The Robot Script is attached to a Game Object.
The Robot is NOT a player.
The Robot is a enemy.
The Robot has a tag called "Enemy", on it.
Thanks for taking your time reading, and for helping!
Answer by Glurth · Mar 05, 2016 at 06:37 PM
It looks like, if the robot is hit you call this line:
RobotScript.instance.Health = 10;
which will set it's health to 10. I suspect you meant to use -= rather than =, so that it will SUBTRACT 10 from the health. e.g.
RobotScript.instance.Health -= 10;
this is equivalent to:
RobotScript.instance.Health = RobotScript.instance.Health - 10;
It is working, but it doesn't actually destroy the object? Like in console it says
Enemy Hit (10) Enemy should be dead (1)
So i hit it 10 times and it said that, but the damage isn't actually taking?