- Home /
why doesn't my reference work?
hi. i have a problem with my references. when i reference an object in the game (Bf) the code works. but when i reference a prefab which i have an instance of the code does not work. what am i missing? Card is the reference to the prefab. and yes both has the referenced script (DropZone)
the code is:
public GameObject Bf; public GameObject Card;
void EnemyTurn()
{
Debug.Log("Enemy's turn");
Bf.transform.GetComponent<DropZone>().typeOfItem = Draggable.Slot.NotActive;
Card.transform.GetComponent<DropZone>().typeOfItem = Draggable.Slot.NotActive;
//turn = true; //make an if statement that asks if movement = 0 and AP = 0 then change turn status to 1
}
Answer by pako · Mar 08, 2018 at 08:19 PM
How do you instantiate the prefab?
This won't work, because it doesn't refer to the instance of the prefab, but to the prefab asset itself, which is not instantiated:
//Card refers to the prefab asset, not an instance of it
Card.transform.GetComponent<DropZone>().typeOfItem = Draggable.Slot.NotActive;
This would work:
//myCard refers to an instance of the Card prefab
myCard = Instantiate(Card, transform.position, transform.rotation); //for example
//...then
myCard.transform.GetComponent<DropZone>().typeOfItem = Draggable.Slot.NotActive;
first of all thank you for your time and help. i have a couple of objects in the game world derived from the prefab, but as you say i try to reference the prefab so that all instances of the prefab would be affected.
i'm not sure when i'm suppose to use the myCard = Instantiate(Card, transform.position, transform.rotation);
since it throws the error of not being able to implicitly convert from Object to GameObject. then i thought ok what if i just call it as an object ins$$anonymous$$d. but the mess with the transform in the second line. `
i'm not sure when i'm suppose to use the myCard = Instantiate(Card, transform.position, transform.rotation);
This is why the first thing I asked you was:
How do you instantiate the prefab?
...and I clearly pointed out:
...//for example
In other words, the information you provided is insufficient to answer the question fully, and I did my best to provide you with useful information, together with an example, knowing that my example is a pointer to the usage, and should not be used "as is", because the exact implementation depends on the remaining part of your code, which you don't show.
you did provide very useful information but i'm trying to wrap my head around it. The way you explain it makes sense but i'm just not seeing the full picture i guess. the full script is as follows`using UnityEngine; using System.Collections; using UnityEngine.UI;
public class Turn$$anonymous$$anager : $$anonymous$$onoBehaviour {
public bool turn = true;
public GameObject Bf;
public GameObject Card;
public GameObject $$anonymous$$$$anonymous$$;
// Use this for initialization
void Start ()
{
//Card = Instantiate (Card, transform.position, transform.rotation);
}
// Update is called once per frame
void Update ()
{
if (turn == true)
{
PlayerTurn();
}
else
{
EnemyTurn();
}
}
IEnumerator timer()
{
Debug.Log("timer started");
yield return new WaitForSeconds(5f);
Debug.Log("Timer stopped");
TurnChange();
}
void PlayerTurn()
{
StartCoroutine("timer");
Debug.Log("Player's turn");
//turn = false; //make an if statement that asks if movement = 0 and AP = 0 or if the "end turn" button was pressed to change turn status to 2
}
void EnemyTurn()
{
StartCoroutine("timer");
Debug.Log("Enemy's turn");
Bf.transform.GetComponent<DropZone>().typeOfItem = Draggable.Slot.NotActive;
//Card.transform.GetComponent<DropZone>().typeOfItem = Draggable.Slot.NotActive;
//turn = true; //make an if statement that asks if movement = 0 and AP = 0 then change turn status to 1
}
public void TurnChange()
{
if (turn == true)
{
turn = false;
StopCoroutine("timer");
}
else
{
turn = true;
StopCoroutine("timer");
$$anonymous$$$$anonymous$$.transform.GetComponent<$$anonymous$$ana>().Add$$anonymous$$ax(1);
$$anonymous$$$$anonymous$$.transform.GetComponent<$$anonymous$$ana>().Refill$$anonymous$$ana();
}
}
}
` the Card GameObject is the prefab itself. and i see from your answer i never put in an actual instantiation but it is because i don't know when i am suppose to do that. if you need the DropZone script aswell i'll provide that too
Additionally:
i try to reference the prefab so that all instances of the prefab would be affected
You can't affect all instances of the prefab at runtime, this is why you must have a reference to the specific prefab instances at runtime, in order to change them individually.
Pako i just want to say thank you so much!! you just broke a $$anonymous$$AJOR obstical i had no idea how to get past! so again thank you so much!