So I’m making a card game (similar to Hearthstone) and got to the point of making the “attacking” script. Every card has a "CardName"Stats script attached to it with its stats. So I want to access those scripts so I can change the Health value when they “fight”.
using UnityEngine;
using System.Collections;
public class TestScriptGetter : MonoBehaviour
{
public RaycastHit hit = new RaycastHit();
public Ray ray;
public float raydistance = 100f;
public string objectname;
public string scriptname;
Component script;
void Update ()
{
if (Input.GetMouseButton (0))
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, raydistance))
{
if (hit.transform.tag == "BSBoardCard")
{
objectname = hit.transform.gameObject.name;
scriptname = hit.transform.gameObject.name + "Stats";
script = GameObject.Find (objectname).GetComponent (scriptname);
if (GameObject.Find (objectname).GetComponents (scriptname)) {
GameObject.Find (objectname).GetComponents (scriptname).Health = 2;
}
// if (script != null)
// {
// script.Health = 2;
// }
}
}
}
}
}
I pretty much understand why it doesn’t work , but is there any way I can fix it ? Different ideas as a whole are accepted as well.