This Demo Code is for editing the value Health on ScriptTwo by reducing the amount from ScriptOne Damage Value.
A similar effect can be done by using a raycast (as you see in common FPS Games) which will not set a target at all, but by checking against hitting a gameobject with a specific script and variable to reduce by.
e.g
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, 100))
print("Hit something");
Reference:
UNITY API (Sorry forum wont let me post the link but search in google unity api raycast)
--------------------------------------------------------------------------------------------------
RPG Targeting system Type Demo Code.
--------------------------------------------------------------------------------------------------
ScriptOne
ScriptTwo
A similar effect can be done by using a raycast (as you see in common FPS Games) which will not set a target at all, but by checking against hitting a gameobject with a specific script and variable to reduce by.
e.g
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, 100))
print("Hit something");
Reference:
UNITY API (Sorry forum wont let me post the link but search in google unity api raycast)
--------------------------------------------------------------------------------------------------
RPG Targeting system Type Demo Code.
--------------------------------------------------------------------------------------------------
ScriptOne
Code:
using UnityEngine;
using System.Collections;
public class ScriptOne : MonoBehaviour {
int Damage = 10;
GameObject Target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
try
{
Target.GetComponent<ScriptTwo>().Health -= Damage;
}
catch
{
if (Target == null)
print("You Do not have a Target!");
else
print("Your Target Does not have ScriptTwo on it!");
}
}
}
}
ScriptTwo
Code:
using UnityEngine;
using System.Collections;
public class ScriptTwo : MonoBehaviour {
public int Health;
void Start()
{
Health = 100;
}
}