My public variables are lost when I convert my game object into a prefab
I have a boost power up that works perfectly fine in the hierarchy. However, when I covert it into a prefab, the variable disconnect?
Incase the code is confusing. I shall explain
powerUpBoostScript - This increases the speed of my player when it hits the power up pickupEffect - This is the animation that instantiates when you pick up the power up Boost - This is the sprite which has the graphic of a boost appear when you pick up the power up
(Code Below)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PowerUpBoost : MonoBehaviour {
public GameObject powerUpBoostScript;
public GameObject pickupEffect;
public GameObject Boost;
public float duration;
private bool boostOn = false;
void OnTriggerEnter2D (Collider2D other)
{
if (other.CompareTag ("Player"))
{
StartCoroutine(Pickup (other));
}
}
IEnumerator Pickup(Collider2D player)
{
//PowerUp explosion (Animation)
Instantiate (pickupEffect, transform.position, transform.rotation);
//Boost jets appear
if (boostOn == false)
{
Boost.SetActive (true);
boostOn = true;
}
//Boost speed increases
PlayerController boostSpeedScript = powerUpBoostScript.GetComponent<PlayerController> ();
boostSpeedScript.speed = 5f;
//Disables the collider2D around PowerUp
GetComponent<Collider2D> ().enabled = false;
GetComponent<SpriteRenderer> ().enabled = false;
yield return new WaitForSeconds (duration);
//Boost speed decreases
boostSpeedScript.speed = 3f;
//Boost jets disappear
if (boostOn == true)
{
Boost.SetActive (false);
boostOn = false;
}
Destroy (gameObject);
}
}
Answer by PersianKiller · May 05, 2018 at 05:35 PM
it happens when the gameObject is only in the scene and doesn't exist inside your assets window.
you have to make (for example (powerUpBoostScript )) a prefab.then assign it inside another prefabs and you can use it when you instantiate it.
Thanks a lot for your help. I have been trying to incorporate what you've said, however Im not quite sure how to have a prefab assigned to another prefab??
hmmm i'm not good at English xD,let me show you an example ,it might be helpful. watch this
Your answer
![](https://koobas.hobune.stream/wayback/20220612162054im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Create gameobject in other scene... 0 Answers
Instantiation of my GameObjects spell (from other script) 0 Answers
problems with instantiating a moving prefab. 0 Answers
ExecuteInEditMode is not working when trying to instantiate a new GameObject [SOLVED] 1 Answer
Instantiated Prefab won't instantiate with a script reference 1 Answer