• 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 Xara · Jan 06, 2012 at 09:02 PM · instantiatespawning

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));
                 
                }
         }
     }
 }
Comment
Add comment · Show 4
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 cj_coimbra · Jan 06, 2012 at 09:12 PM 0
Share

I am not sure, but maybe your ray is hitting several objects hence instantiating several others...

avatar image DaveA · Jan 06, 2012 at 09:49 PM 1
Share

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.

avatar image Bunny83 · Jan 07, 2012 at 12:35 AM 0
Share

@DaveA: Good catch. A common mistake :D

avatar image Xara · Jan 07, 2012 at 12:54 AM 0
Share

@DaveA

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?

1 Reply

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

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.

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 Xara · Jan 07, 2012 at 02:37 AM 0
Share

@aldonaletto

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.

avatar image aldonaletto · Jan 07, 2012 at 05:34 AM 0
Share

Weird thing: Destroy(hit.gameObject) should work... but if it's working your way, ok!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

8 People are following this question.

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

Related Questions

How Do I Add An Instantiated Object To An Array? 3 Answers

Add an offset distance between 2 spawned objects 1 Answer

How would I create a script that spawns objects more frequently as time goes on? 3 Answers

How to Make an Expanding Spawn Area for Prefabs? 2 Answers

spawning script not working 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