- Home /
Instantiated GameObject does not have variables
Im working on a weapon pickup and drop system but the dropped weapon clone does not allow to pick it up again because the variables arent assigned to the clone! How can I fix that? I cant use find because the variables are not defined.
var wepclone : GameObject;
var weapon1 : GameObject;
var weapon2 : GameObject;
var inTrigger : boolean = false;
var picktext : GameObject;
var Spawn : Transform;
var wepdrop : GameObject;
function Start (){
picktext = GameObject.Find("PickText");
Spawn = transform.Find("wepdropspawn").GameObject;
picktext.SetActive(false);
}
function Update (){
if(!weapon1.active){
if(!Input.GetButton("left shift")){
if(inTrigger){
picktext.SetActive(true);
if(Input.GetKeyUp("e")){
Destroy(wepclone);
weapon1.SetActive(true);
weapon2.SetActive(false);
picktext.SetActive(false);
var wepdropclone : GameObject = Instantiate(wepdrop,Spawn.position,Spawn.rotation);
}
}
else if(!inTrigger){
picktext.SetActive(false);
}
}
}
}
function OnTriggerEnter (pickarea : Collider) {
if(pickarea.tag == "Player"){
inTrigger = true;
}
}
function OnTriggerExit (pickarea : Collider){
if(pickarea.tag == "Player"){
picktext.SetActive(false);
inTrigger = false;
}
}
When you Instantiate you are $$anonymous$$aking a new Copy of the prefab in your assets folder, not the gameobject in your scene. if you want the variables to be assigned to it, you need to assign them in the prefab, then they will be there automatically. if that is not an option, then you can assign them like
wepdropclone.GetComponent<ScriptName>().publicVariableName = webdrop.GetComponent<ScriptName>().publicVariableName;
(sorry this is in c sharp which i know probably isnt too helpful but im not too javascript savy)
As I mentioned, the problem is that the variables are undefined. It is different depending on which objects its attached
Im not quite sure what you are asking here. As i said in the above comment, you need to write code that assigns the empty variables to the values used. If you need to reference the parent you would do something like webdropclone.transform.parent.GetComponent...etc.
Your answer
Follow this Question
Related Questions
How to differentiate between prefabs on collision. 1 Answer
C# Destroy Prefab on Pickup 1 Answer
Compiler error FPSplayer script 2 Answers
Weapon pick up 3 Answers
Problem With Weapon Firing Mechanism 1 Answer