- Home /
Destroying Prefab Also Destroys Prefab Reference
Whenever I shoot a bullet in my scene, my script replaces the prefab reference of the bullet for bullet (clone). Eventually it becomes something like bullet (clone) (clone) (clone) (clone) (clone). When the most recently created bullet is destroyed, the reference is destroyed with it. With no bullet reference in the script, I can't shoot, and I get an error that says MissingReferenceException.
I don't know what the problem is, and I tried looking it up but the only answer was in JS, which I can't read.
I'm speculating that the problem is I am instantiating the prefab ITSELF, not a clone of the prefab. Unfortunately I have no idea how to change that, and I'm not positive that's the problem.
using UnityEngine;
using System.Collections;
public class PlayerShot : MonoBehaviour {
public GameObject projectilePrefab;
private float timeclicked;
public Transform thispos;
public Transform thisposr;
public float speed = 1.5f;
public Vector3 target;
public static int leftshot;
void Start () {
target = transform.position;
}
void Update () {
if (Input.GetKey (KeyCode.Space)&& Time.time > timeclicked && PlayerController.left == false && Time.time > PlayerController.jumptime) {
projectilePrefab = Instantiate(projectilePrefab, thispos.position, thispos.rotation) as GameObject;
timeclicked = Time.time + 0.2f;
leftshot = 1;
}
if (Input.GetKey (KeyCode.Space)&& Time.time > timeclicked && PlayerController.left == true && Time.time > PlayerController.jumptime) {
projectilePrefab = Instantiate(projectilePrefab, thisposr.position, thisposr.rotation) as GameObject;
timeclicked = Time.time + 0.2f;
leftshot = -1;
}
}
}
using UnityEngine;
using System.Collections;
public class SpeechProj : MonoBehaviour {
private Vector3 shotTarget;
public Rigidbody2D thisrigid;
public Vector3 target;
public float xGo;
public float yGo;
void Start ()
{
rigidbody2D.AddForce (Vector3.right * 1200 * PlayerShot.leftshot);
target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
// Update is called once per frame
void Update ()
{
}
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Enemy" || coll.gameObject.tag == "Blocker")
{
Destroy(this.gameObject);
}
}
}
Answer by AyAMrau · Sep 09, 2014 at 02:46 AM
Don't assigning the new instance to the projectilePrefab variable. Make a separate variable for the spawned object and assign to that.
I don't know how to do that + your answer is a little vague. Can you provide me with a different script or give me the instructions in Unity?
$$anonymous$$ake a new variable to store the spawned object
private GameObject instance;
When Instantiate is called assign the result to the instance variable ins$$anonymous$$d of the prefab variable.
instance = Instantiate(projectilePrefab, thispos.position, thispos.rotation) as GameObject;
There is really nothing vague about that. If you are unsure about scripting and related ter$$anonymous$$ology there is a lot of beginner material available on the internet.
Its easier to understand once I see the script. That solves the problem. Thanks for the answer.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Create destroyed prefab 1 Answer
My chain hit goes to the center of the map, why? 0 Answers
gameObject are not referenced 2 Answers
Projectile isn't reaching my target. 1 Answer