Hello, I'm making a card game with an int attack for each card. I initialized the int attack at the begining of the script, so when I create a gameobject, I can enter the attack in parameters. I'd like to print the attack when I click on the card but I don't know how to do it. I did that int attack; Ray ray; RaycastHit hit; if (Physics.Raycast (ray, out hit) && Input.GetKeyDown (KeyCode.Mouse0)) { Debug.Log(hit.collider.gameObject.attack); }
So you have cards and each card has a script on it and you initialize the values in the inspector ? if so then try: Code (CSharp): using UnityEngine; using System.Collections; public class Example : MonoBehaviour { int Attack; void Update() { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit, 100) && Input.GetMouseButton(0)) { GameObject Card = hit.transform.gameObject; AttackScript attackscript = Card.GetComponent<AttackScript>(); Debug.Log(attackscript.Attack); } } } This will get the value from the script on the card and print the value. Make sure the int value is public.