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);
}
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.
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();
}
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.
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!
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()?
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
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.
Your answer
Follow this Question
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