- Home /
Selecting the currently active gameobject in an array to move its values
I am currently working on a card game where I have an array of card colors (2 cards per color) and I want to move said card into their correct color field.
Ex. If a random yellow card spawns then I place it in the yellow area.
Right now I made the script to create a first card at the start of my game and now I am thinking that I need to make a new variable and assign that to the currently instantiated card color from my array list.
Here is my code and I put in asterisks & capital (line 53) what I want it to do in case I am not being clear enough.
using UnityEngine;
using System.Collections;
public class CardSpawner : MonoBehaviour {
public GameObject[] cardDeck = new GameObject[8]; //array that contains all card colors
//Touch Control variables
//public Transform player; // Drag your player here
private Vector2 fp; // first finger position
private Vector2 lp; // last finger position
private float angle;
private float swipeDistanceX;
private float swipeDistanceY;
//the 4 areas you must correctly place the colored cards in - transform to move the card to their position
public Transform greenArea;
public Transform yellowArea;
public Transform redArea;
public Transform blueArea;
private GameObject activeCard; //the current card on screen
void Start () {
Instantiate(cardDeck[Random.Range(0, cardDeck.Length)], transform.position, transform.rotation); //spawns the first card randomly
}
void Update () {
//This handles the touch controls & what happens when a swipe in a direction is made
foreach(Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
fp = touch.position;
lp = touch.position;
}
if (touch.phase == TouchPhase.Moved )
{
lp = touch.position;
swipeDistanceX = Mathf.Abs((lp.x-fp.x));
swipeDistanceY = Mathf.Abs((lp.y-fp.y));
}
if(touch.phase == TouchPhase.Ended)
{
angle = Mathf.Atan2((lp.x-fp.x),(lp.y-fp.y))*57.2957795f;
if(angle > 60 && angle < 120 && swipeDistanceX > 40 )
{
print ("right");
//cardDeck[1].transform.position = redArea.transform.position;
//******CARDDECK[ACTIVE] = REDAREA TRANSFORM.POSITION*******
//player.Rotate(0,45,0);
}
if(angle > 150 || angle < -150 && swipeDistanceY > 40)
{
print ("down");
//player.position+=new Vector3(0,-2,0);
}
if(angle < -60 && angle > -120 && swipeDistanceX > 40)
{
print ("left");
//player.Rotate(0,-45,0);
}
if(angle > -30 && angle < 30 && swipeDistanceY > 40)
{
print ("up");
//player.position+=new Vector3(0,2,0);
}
}
}
}
}
Your answer
Follow this Question
Related Questions
IndexOutOfRangeException: Array index is out of range when using an Array and instantiating 2 Answers
Array with game object: object reference not set to an instance of an object 1 Answer
How to Instantiate a Different Game Object After Getting to the End of an Array? 1 Answer