Search Unity

print

Discussion in 'Scripting' started by leogre, Mar 2, 2015.

  1. leogre

    leogre

    Joined:
    Mar 2, 2015
    Posts:
    4
    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);
    }
     
  2. Jmanpie

    Jmanpie

    Joined:
    Oct 7, 2012
    Posts:
    132
    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):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Example : MonoBehaviour {
    5.     int Attack;
    6.     void Update() {
    7.         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    8.         RaycastHit hit;
    9.         if (Physics.Raycast(ray, out hit, 100) && Input.GetMouseButton(0))
    10.         {
    11.             GameObject Card = hit.transform.gameObject;
    12.             AttackScript attackscript = Card.GetComponent<AttackScript>();      
    13.             Debug.Log(attackscript.Attack);
    14.         }    
    15.     }
    16. }

    This will get the value from the script on the card and print the value.
    Make sure the int value is public.