How can I get gameObject from hirerchy to the public GameObject slot of a script attached to a prefab?
When a script is attached to a prefab and there's pulic gameobject slot for it, only prefab gameobjects can be in that slot, not the one in the actual scenes.
But, what I want to do is get the specific object that is in the scene.
How can I dragg the gameObject in the hierchy and drop it in the gameobject slot of a prefab's script?
I did something like this before.There was a stone and player was able to pick it up.
//stone script
public GameObject StonePlace; //it's in my stone script.
public bool PlayerPickedUpTheStone;
void Update(){
if(PlayerPickedUpTheStone){
transform.position=StonePlace.transform.position;
}
}
the problem was that the stone was a prefab, but my player wasn't,so I wrote a script like this
//player script
public GameObject PlayerHand;// then I assigned it
and again in the stone script
void OnTriggerEnter2D(Collider2D other){
if (other.CompareTag ("player")) {
StonePlace=other.GetComponent<PlayerScripts> ().PlayerHand;
}
}
so when the stone collides with the player,PlayerHand of the Player script will be the playerhand of the stone script. :).
Wow thanks for the help! It helped me a LOT!!!
Your answer
Follow this Question
Related Questions
Changing a Prefab's Text component seems to be broken. 1 Answer
Prefabs and public variables 1 Answer
UnassignedReferenceException: The variable has not been assigned, even though it has. - c# 3 Answers
Changing multiple identical gameobjects into one prefab? 1 Answer
How do you set Prefabs into a GameObject array that will activate/deactivate when called? 1 Answer