- Home /
Too many instances
I am trying to replace a prefab with another. I am using Instantiate and Destroy. I am using Raycast to target the prefab with the mouse.
It works but it instantiates many prefabs instead of just one. And the number of instances that it instantiates varies from 6 instances to 1.
Can someone please help me to find the problem?
C# code:
using UnityEngine;
using System.Collections;
public class Srp_LandPositioner : MonoBehaviour {
// Found Mouse select code here:
// http://answers.unity3d.com/questions/23711/destroy-object-on-mouse-click.html
// Update is called once per frame
void Update()
{
//this if check for the mouse left click
if(Input.GetMouseButtonUp(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//this if checks, a detection of hit in an GameObject with the mouse on screen
if(Physics.Raycast(ray, out hit))
{
Debug.DrawRay (ray.origin, hit.point);
print (hit.collider.gameObject.name);
GameObject newApple = Instantiate(GameObject.Find("Pre_Apple"), hit.collider.gameObject.transform.position, hit.collider.gameObject.transform.rotation) as GameObject;
newApple.name = "Apple " + newGameLand.transform.position.x + ", " + newGameLand.transform.position.z;
Destroy(GameObject.Find(hit.collider.gameObject.name));
}
}
}
}
I am not sure, but maybe your ray is hitting several objects hence instantiating several others...
How many objects does this script exist on? It looks like it should instantiate one per mouse-up, but if it's on more than one object, it will do this for each one.
Thank you.
You are correct. I have another scrip that makes 5 pears. Each pear generated will have the scrip above in it.
The idea as follows: There is an apple surrounded by 5 pears in the scene. This scrip is connected to all the pears. I click on a pear and it gets replaced by an apple. Later I will add to this script to add also 5 more pears around the generated apple but not in the places where there is already and apple or a pear. As you can tell it grows and grows.
So this script needs to be on all pears.
Is there a way around this problem?
Answer by aldonaletto · Jan 07, 2012 at 01:44 AM
You should modify the script a little, and attach it to a single object - the camera, for instance - thus you would not have multiple instances running around:
using UnityEngine; using System.Collections;
public class Srp_LandPositioner : MonoBehaviour {
GameObject apple; // drag apple prefab here GameObject pear; // drag pear prefab here
void Update(){ if(Input.GetMouseButtonUp(0)){ Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if(Physics.Raycast(ray, out hit)){ if (hit.transform.CompareTag("Pear")){ GameObject newApple = Instantiate(apple, hit.transform.position, hit.transform.rotation); newApple.name = "Apple " + newGameLand.transform.position.x + ", " + newGameLand.transform.position.z; Destroy(hit.gameObject); } } } } } Notice that the script above is instantiating a prefab, not a scene object: to make the Pre_Apple a prefab, just drag it to the Project view.
It worked. Thank you very much.
I had to change your code a bit to look like my previous code to get it to work but the idea to put it in the camera and adding "public GameObject apple;" worked.
I re-added "Debug.DrawRay (ray.origin, hit.point);"
and also re-added "Destroy(GameObject.Find(hit.collider.gameObject.name));" I have seen many examples with just "Destroy(hit.gameObject);" but I can't get it to work;
Also needed to add "(GameObject)" before Instantiate command.
Again thank you. I could not get my head around it.
Weird thing: Destroy(hit.gameObject) should work... but if it's working your way, ok!
Your answer
Follow this Question
Related Questions
What's wrong with my script? 1 Answer
extra GameObjects Spawning 2 Answers