Assign a prefab in the inspector disappear when the game start

Hi everybody

I can´t resolve this issue by myself. To sum up: I have a prefab shot, this prefab has a script of the behaviour of the shot called shot_behaviour, a sprite renderer, an animation and a 2DpoligonColider. Then, i have an item in the world called generator_shot, the generator shot has a public method of the shot_behavior type. In the inspector, i can assign without any problem:
2127497--139921--Befor_game_Start_inspector.png
However, when the game start, the prefab disappear from the inspector:

2127497--139922--After_game_Start_inspector.png
Obviosly, when i press the fire button, the console send a message error that cant find the item:

I tried resolve this issue in two ways:

  • Instead of create a prefab with that elements, I only create a cube element in the world, i assign to it the shot_behaviour, and then assigned to the gun_shoot_generator_behaviour, but also dissapear
  • The other form is create all the elements in the script gun_shoot_generator_Behaviour, but this method implies i cant reuse this elements and always have to be at the same form. I want to create different shot type depend on who shoot.

Can someone help my to resolve this?

Thank you in advance

Post your code please. From the looks of your screenshots, you haven’t assigned the prefab to a GameObject variable (or other variable, as applicable), so the script doesn’t know what you want to spawn. Just a guess, but a look at your code will tell us for sure.

1 Like

the shoot_Behaviour script:

using UnityEngine;
using System.Collections;

public class Shoot_Behaviour : MonoBehaviour {
    public float shoot_speed;
    public float live_shoot_time;

    void Start () {
        StartCoroutine ("live_until_destroy");
    }

    void Update () {
        this.transform.position = new Vector3 (this.transform.position.x, this.transform.position.y + (shoot_speed * Time.deltaTime), this.transform.position.z);
    }

    public IEnumerator live_until_destroy(){
        yield return new WaitForSeconds (live_shoot_time);
        Destroy (this.gameObject);
    }
}

and the gun_shoot_generator_behaviour

using UnityEngine;
using System.Collections;

public class Gun_Shoot_Generator_Behaviour : MonoBehaviour {

    public Shoot_Behaviour shoot_emiter;
    private AudioSource shoot_plasma_sound;
    private bool left_right_side_selected=false;
    public float separate_between_left_right_dual_guns = 0.86f;


    // Use this for initialization
    void Start() {
        shoot_emiter = this.gameObject.GetComponent <Shoot_Behaviour> ();
        shoot_plasma_sound = this.gameObject.GetComponent <AudioSource> ();
        NotificationCenter.DefaultCenter ().AddObserver (this, "Shoot_notification_receiver");
    }

    public void Shoot_notification_receiver(Notification notificacion){
        this.shoot_plasma_sound.Play ();
        Instantiate (shoot_emiter, this.gameObject.transform.position, Quaternion.identity);
        if (left_right_side_selected == false) {
            this.gameObject.transform.localPosition = new Vector3 (this.gameObject.transform.localPosition.x+separate_between_left_right_dual_guns, this.transform.localPosition.y, this.transform.localPosition.z);
            this.left_right_side_selected = true;
        } else {
            this.gameObject.transform.localPosition = new Vector3 (this.gameObject.transform.localPosition.x-separate_between_left_right_dual_guns, this.transform.localPosition.y, this.transform.localPosition.z);
            this.left_right_side_selected = false;
        }
    }
}

OK, in the “gun_shoot_generator_behaviour” script it looks like you’re trying to Instantiate shoot_emitter, but shoot_emitter looks to be a “Shoot_Behavior” script, not an actual game object. You’ll need to create an actual game object with that script on it, set it up as a prefab, alter the “gun_shoot_generator_behaviour” to know that it’s a game object, and then Instantiate that prefab instead…

1 Like

In the Start() function you are re-assigning shoot_emiter.

Unity propably doesn’t find it and therefore sets the public variable to null.

And this:

Instantiate (shoot_emiter, this.gameObject.transform.position, Quaternion.identity);

shouldn’t work this Instantiate usually takes a GameObject, not a script like Shoot_Behaviour.

1 Like

that is!!! thank you very much, i do that changes, and fix the problem

using UnityEngine;
using System.Collections;

public class Gun_Shoot_Generator_Behaviour : MonoBehaviour {

    public GameObject shoot_emiter;
    private AudioSource shoot_plasma_sound;
    private bool left_right_side_selected=false;
    public float separate_between_left_right_dual_guns = 0.86f;


    // Use this for initialization
    void Start() {
        //shoot_emiter = this.gameObject.GetComponent <GameObject> ();
        shoot_plasma_sound = this.gameObject.GetComponent <AudioSource> ();
        NotificationCenter.DefaultCenter ().AddObserver (this, "Shoot_notification_receiver");
    }

    public void Shoot_notification_receiver(Notification notificacion){
        this.shoot_plasma_sound.Play ();
        Shoot_Behaviour shot = Instantiate (shoot_emiter, this.gameObject.transform.position, Quaternion.identity) as Shoot_Behaviour;
        if (left_right_side_selected == false) {
            this.gameObject.transform.localPosition = new Vector3 (this.gameObject.transform.localPosition.x+separate_between_left_right_dual_guns, this.transform.localPosition.y, this.transform.localPosition.z);
            this.left_right_side_selected = true;
        } else {
            this.gameObject.transform.localPosition = new Vector3 (this.gameObject.transform.localPosition.x-separate_between_left_right_dual_guns, this.transform.localPosition.y, this.transform.localPosition.z);
            this.left_right_side_selected = false;
        }
    }
}
1 Like

Wonderful! So glad it worked out :slight_smile: Have fun & happy creating!

1 Like