- Home /
Instantiated object not showing in scene or hierarchy
So I'm trying to have my player equip a weapon if he picks it up by instantiating the weapon object as a child of the player's arms (this is a 2D game btw) and destroying the weapon he currently has equipped. The weapon object, however is not instantiating. Currently I'm referencing the weapon prefab through Resources.Load, but I've also tried dragging the prefab into the inspector just to check that I wasn't somehow grabbing the wrong path name and that didn't work either.
I know it's trying to grab the correct object because my print statements are coming back correct. It just won't show up in the hierarchy or scene. This script is on the weapon drop item that sits on the ground. When the player presses Q near it, one of the functions it runs is the EquipWeapon function.
Why isn't it instantiating correctly?
void Start() {
weaponPrefab = Resources.Load("Prefabs/Items/PlayerWeapons/" + gameObject.name) as GameObject;
weaponID = weaponPrefab.GetComponent<Weapon>().weaponID;
playerArm = GameObject.Find("Arm");
}
public void EquipWeapon(int id)
{
Item weaponToAdd = itemDatabase.FetchItemByID(id);
print("Weapon to add: " + weaponToAdd.ItemName);
if (hotbarScript.currentlyEquippedWeapon != null)
{
Destroy(hotbarScript.currentlyEquippedWeapon);
GameObject weapon = Instantiate(weaponPrefab);
weapon.name = weaponToAdd.ItemName;
weapon.transform.SetParent(playerArm.transform);
hotbarScript.currentlyEquippedWeapon = weapon;
print("Currently equipped weapon: " + hotbarScript.currentlyEquippedWeapon.name);
}
}
private void OnTriggerStay2D(Collider2D collision)
{
if (collision.tag == "Player")
{
GetComponent<SpriteRenderer>().material = highlightMaterial;
if (Input.GetKeyDown(KeyCode.Q))
{
hotbarScript.AddItemToInventory(weaponID);
EquipWeapon(weaponID);
}
}
}
Answer by Strixie13 · Jul 22, 2018 at 01:11 AM
I think your cast is in the wrong place. Try weaponPrefab = (GameObject)Resources.Load("Prefabs/Items/PlayerWeapons/" + gameObject.name);
Nope, that works the same exact way. :( Thanks for the suggestion though!
Looked back over it, might be because you are loading to a variable, then trying to instantiate the variable. Try instantiating right away to the object variable.
void Start() {
weaponPrefab = Instantiate((GameObject)Resources.Load("Prefabs/Items/PlayerWeapons/" + gameObject.name));
weaponID = weaponPrefab.GetComponent<Weapon>().weaponID;
playerArm = GameObject.Find("Arm");
}
Answer by kalen_08 · Jul 22, 2018 at 01:36 AM
What about at the start of the game when the current weapon is null?? If it is null you still want to instantiate the weapon correct? if its not null all you want to do different is just destroy the current weapon.
public void EquipWeapon(int id)
{
Item weaponToAdd = itemDatabase.FetchItemByID(id);
print("Weapon to add: " + weaponToAdd.ItemName);
if (hotbarScript.currentlyEquippedWeapon != null)
{
// if there currently is a weapon destroy it.
Destroy(hotbarScript.currentlyEquippedWeapon);
}
// now that no matter what there is no weapon...
// Instantiate the new weapon.
GameObject weapon = Instantiate(weaponPrefab);
weapon.name = weaponToAdd.ItemName;
weapon.transform.SetParent(playerArm.transform);
hotbarScript.currentlyEquippedWeapon = weapon;
print("Currently equipped weapon: " + hotbarScript.currentlyEquippedWeapon.name);
}
I think that should do it. if not let me know.
Good suggestion, but that didn't work. I just don't understand...even if I try just instantiating an object from the PlayerWeapons folder in the Start() method, the object won't instantiate. And it doesn't work for any gun in the folder, yet it works just fine for the enemy weapons, even though the only difference between them is the shooting script attached...
Your answer
Follow this Question
Related Questions
How can I Instantiate two new GameObjects at the same time? (Attempting Asteroids style game) 2 Answers
InvokeRepeating time doesn't sync within the same update function 2 Answers
How to load a file during runtime? (Resources folder doesn't work) 0 Answers
Moving a object randomly without it being inside a wall 1 Answer
RigidBody2D velocity speed difference between devices 0 Answers