How do I reference an Instantiate object?
Hello, I am new on programming. I'm working on a small game (more like a learning experience), in unity, and I have some questions.
First of all here is the basic concept of the game: it's a turn-based card game, with movement. The cards are randomly given and the player choses where to put them in the board. After the player ends the turn, the cards shall move. If there is an enemy card in front, the card should attack the other card.
The main problem is not the code it self... Is that I have no idea how to detect a card, or how to reference its specific health or anything at all. I know how to make instances and other stuff like that. But it gets confusing to work with instances and whatever are "scriptable objects". There is also the fact that there can be two of the same cards at the same card at the board.
If it isn't asking for too much, I would also appreciate if you could explain me how I can attribute a "team" to the cards without creating new ones (enemy or ally, for example). That would help in case I want to identify if one of the cards is ally so that it would not take damage (I think I can make the code for taking damage, only need a way to identify the team).
I have a grid manager, and a game manager with all of the game states.
I'm sending the script for the instances of the cards on the board (there is also a game object for the cards on the hand, but I don't think that is relevant).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class OnCard : MonoBehaviour
{
public static OnCard Instance;
[SerializeField] private Transform card;
private Vector3 otherCardPosition;
private GameObject cardToAttack;
private Vector3 savedPosition;
private Vector3 futurePosition;
private int damage;
private void Awake() { //just making Instances so it is possible to referance this script later
Instance = this;
}
void Start()
{
//set the position of the card at the center (temporary)
card.transform.position = new Vector3((float)GridManager.Instance._width/2 -0.5f,(float)GridManager.Instance._height/2 -0.5f, 0);
savedPosition = transform.position;
}
public void CardMovement()
{
futurePosition = new Vector3(transform.position.x, transform.position.y + 1, transform.position.y);
if(futurePosition == otherCardPosition)
{
Attack(damage, cardToAttack); //how would I send wich card it is in front?
return;
}
transform.position = futurePosition;
}
private void Attack(int damage, cardToAttack)
{
cardToAttack.health = cardToAttack.health - 1;
//this is just an exemple
}
}
I would be very thankful if any of you could help me.
Thank you for your time.
Answer by rh_galaxy · May 13 at 01:54 PM
To answer the question of how to reference an Instantiated object. One way is to keep a list where you do instantiate, so you can loop through the objects and do things. Also a common way to Initialize an Instantiated object is to add an Init() method to set different parameters/behaviors. Another way is singleton, which is what you began with Instance = this;
, but that way you can't have more than one object, so that is probable something reserved for a GameManager and CameraHolder or AudioManager, not each of the cards in the game.
public class Player : MonoBehaviour
{
public Card cardBase; //original, can be a prefab set in inspector
List<Card> cardList;
void Awake()
{
Card card = Instantiate(cardBase, this.transform);
card.Init(anything you want really...);
cardList.Add(card);
//...
}
}