Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Crewdog · Oct 18, 2016 at 02:44 PM · instantiateclonedestroy object

Destroy specific clone

I have looked over some of the other questions related to this but can't seem to find one that actually fits my situation. I figure it will be simple and i'm just not seeing it.

In my game, the player clicks to instantiate player prefab objects. If those player objects collide with certain other objects, i would like to destroy that particular player object.

Currently, if any of the player objects collide, the player is no longer able to click and instantiate new player objects and i get the following:

MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.

and

MissingReferenceException: The object of type 'Rigidbody' has been destroyed but you are still trying to access it.

This is in the script for the non-player object

  void OnTriggerEnter(Collider other)
     {
             Destroy(other);
     }



Comment
Add comment · Show 2
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 bellicapax · Oct 18, 2016 at 02:55 PM 0
Share

Can you post the script that spawns your player prefabs and a shot of the player prefab in the inspector? It sounds like you are destroying what you are cloning off of, but it's hard to tell without more information.

avatar image Crewdog bellicapax · Oct 18, 2016 at 10:32 PM 0
Share

This may actually help me with another problem. $$anonymous$$y spawn code is below. The objective is to place a planet (player) object when the mouse button is pressed. The player can then draw back while holding the button and on release, the object should be launched forward by adding a force. On occasion, the player objects fail to return is$$anonymous$$inematic to false and thus get stuck.

     void Update()
     {
         // Place Planets
         if (Input.Get$$anonymous$$ouseButtonDown(0))
         {
             origin = FindPosition();
             player = Instantiate(player, origin, Quaternion.identity) as GameObject;
             prb = player.GetComponent<Rigidbody>();
             prb.is$$anonymous$$inematic = true;
         }
     }
 
     void FixedUpdate()
     {
         // Apply force and launch Planets
         if (Input.Get$$anonymous$$ouseButtonUp(0))
         {
             prb.is$$anonymous$$inematic = false;
             CalcForce();
             prb.AddForce(force * prb.mass);
             // prb.WakeUp();
         }
  

alt text

gravity-player.png (55.4 kB)

3 Replies

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

Answer by Crewdog · Nov 12, 2016 at 10:39 PM

The following solves my problem of some objects remaining in a isKinematic state after i release the mouse button. I moved all button checks from FixedUpdate to Update with a boolean check in fixedupdate to prevent forces being applied multiple times.

 private bool pforceApplied = true;    

 void Update()
     {
         // Place Planets
         if (Input.GetMouseButtonDown(0))
         {
             positionOne = FindPosition();
             playerPlanet = Instantiate(planet, positionOne, Quaternion.identity) as GameObject;
             prb = playerPlanet.GetComponent<Rigidbody>();
             prb.isKinematic = true;
         }
 
         // Calculate force applied to planet
         if (Input.GetMouseButtonUp(0))
         {
             CalcForce();
             pforceApplied = false;
             prb.isKinematic = false;
         }
 }
 
     void FixedUpdate()
     {        
         // Apply force and launch planet
         if (pforceApplied == false)
         {
             prb.AddForce(force);
             pforceApplied = true;
         }
 }

To fix my problem with the clones being destroyed with no ability to create new ones, i did the following. Which works with out issue so far. on occasion it throws an error that last briefly then clears it self. not totally sure why yet.

 using UnityEngine;
 using System.Collections;
 
 public class DestroyByContact : MonoBehaviour {
 
     // Destory any object that collides with this one
     void OnTriggerEnter(Collider other)
     {
     Destroy(other.gameObject);
     }
 
 }

I never really had luck adding the AddForce script and method to the actual player object. It would apply the force but then apply the force to every object the player instantiated. The current fix allows the player to instantiate the object with the mouse, draw back and release applying an initial force only.

Comment
Add comment · 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
0

Answer by OusedGames · Oct 19, 2016 at 12:31 AM

You should create a script that adds force and put on your prefab!

  • You do not need that on update just on Awake will do the job

  • Once it is instantiated it will enter the Awake ()

-Like this

 void Awake() {
         rigid = gameObject.GetComponent <Rigidbody>();
     rigid.useGravity = false;
     rigid.isKinematic = false;
 
     rigid.AddForce (transform.forward *speed); //****
  }

  • On the gameObject that shoots (player) you check on Update the click

  • AND instantiate

  • Once it is instantiated it will enter the Awake () and add the force to the prefab

Hope it works!

Comment
Add comment · Show 2 · 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 Crewdog · Oct 19, 2016 at 01:41 AM 0
Share

I'm sorry, you may have to rephrase this for me. So i currently have a launcher gameObject that finds the players mouse position, instantiates and places the player object, makes the player object is$$anonymous$$inematic = true, calculates a force, makes is$$anonymous$$inematic = false, then applies the force. This works 90% of the time.

Would you suggest having the launcher gameObject only place the player object. Then have a script on the player object that finds the mouse position and applies the force via Awake() ins$$anonymous$$d of FixedUpdate()?

avatar image OusedGames · Oct 20, 2016 at 01:44 AM 0
Share

You can find some videos on youtube $$anonymous$$ching how to instantiate but i will summarize for you!

  • Create a script that adds force and write that inside Awake()

  • Just like my first answer

  • Put that script on your instantiated prefab


  • And use an Script, running Update() to check the $$anonymous$$ouse Click

  • Put this script on the game object that shoots

  • This will be the script that instantiate the objects

  • Every time an object is instantiated it will run the Awake code, adding force

Hope it works

avatar image
0

Answer by sniper02311 · Jan 15, 2019 at 09:57 AM

I am trying to find a way around a similar issue, and I know that the error: MissingReferenceException: The object of type 'Rigidbody' has been destroyed but you are still trying to access it. is because it is deleting the first instantiated object, but is still keeping track of how many have been created. Therefore it shows that a gameobject is missing(behind doors), and tries to keep accessing it IN ORDER :/. I know that is not much help but it is something I found out that has some relation to an original issue you had.

I am trying to figure as well how to fix this by deletion on my own terms, and not in order of creation like Unity is forcibly doing.

Comment
Add comment · 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

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

instantiate and delete trouble with my NPC 0 Answers

cant get my clone to move, help,,Trying to get my clone to move 0 Answers

How can I change this script so that each instantiated prefab spawns a set amount faster 0 Answers

How to save and load all gamebjects which are cloned at runtime???? 0 Answers

Making clones of a GameObject but then the original gets destroyed 0 Answers


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