Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Joseph Lerner · Sep 09, 2014 at 02:42 AM · c#prefabdestroyreferenceprojectile

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);
             }
     }
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Joseph Lerner · Sep 09, 2014 at 03:17 AM 0
Share

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?

avatar image AyAMrau · Sep 09, 2014 at 03:28 AM 0
Share

$$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.

avatar image Joseph Lerner · Sep 10, 2014 at 12:47 AM 0
Share

Its easier to understand once I see the script. That solves the problem. Thanks for the answer.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges