- Home /
prefabs not saving GameObject variables
so i have a water bottle that i want to spawn and when i click it adds a panel prefab to a spacer ui (just another panel but with a grid layout)and then deletes the game object the water bottle spawns in using Instatiate it comes from a prefab under the prefab i have gameobject variablesand they dont save so i inserted some code that finds the gameobject and loads it but it wont load.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Pickupable : MonoBehaviour {
public string ItemType;
public GameObject spacer;
public Transform WaterBottleIcon;
Transform WaterBottleIconClone;
void Start(){
spacer = GameObject.Find ("spacer"); //supposed to find the spacer gameobject but it wont work.
}
void Update(){
}
void pickUp (){
spacer = GameObject.Find ("spacer");
if (ItemType == "waterBottle") {
WaterBottlePickup ();
Debug.Log ("picked up");
}
}
void WaterBottlePickup(){
WaterBottleIconClone = Instantiate (WaterBottleIcon) as Transform;
WaterBottleIconClone.transform.SetParent (spacer.transform);
Destroy (this.gameObject);
}
}
Are you getting any error on console? spacer its not founding? in that case, the gameObject is actived?
no it lets me start the game but in the inspector spacer never loads when i click the water bottle it gives null refrence error due to the gameobject variable being empty.
If spacer gameObject are not active, spacer = GameObject.Find ("spacer"); will be not found if spacer gameObject is a child of any object spacer = GameObject.Find ("spacer"); will be not found.
I supose spacer have a parent (Canvas), right?
spacer = GameObject.Find ("nameOfParent").Find("spacer");