• 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 LockerAT · May 12, 2019 at 05:51 PM · gameobjectinstantiatecreate

The problem with FindObjectOfType and Instantiate.Why is not one object created, but a multiplied copy?

Guys, Problem with FindObjectOfType with Instantiate. There are two classes in class1; I call a method to create an Instantiate object in class2. All classes on different objects. I call through FindObjectOfType The problem is that in this case I create not one object each time, but multiplied by the number of created (1 2 4 * 8, etc.). Who knows what the Trouble?

https://pastebin.com/7mzGBZTp

Comment
Add comment · Show 1
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 Bonfire-Boy · May 12, 2019 at 10:40 PM 0
Share

I think you might get more help if you actually post your code here. Use the 101010 button

1 Reply

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

Answer by Magso · May 12, 2019 at 06:49 PM

It would be safer to have a boolean change when you press R and make Respawn a coroutine with a while loop.

 void Start()
     {
         StartCoroutine("Respawn");
     }
     void Update()
     {
   //One object is created directly
         if (Input.GetKeyDown(KeyCode.R))
         {
             respawnBool = true;
         }
     }
     IEnumerator Respawn()
     {
            while(true)
            {
                    yield return new WaitForSeconds(0.1f);
                    if(respawnBool)
                    {
                           float a = 0;
                           float b = 0;
                           Vector3 respawn = new Vector3(a, b, 0);
                           int i = Random.Range(0, groups.Length);
                           Instantiate(groups[i], respawn, Quaternion.identity);
                           respawnBool = false;
                    }
            }
     }

Comment
Add comment · Show 1 · 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 LockerAT · May 12, 2019 at 08:23 PM 0
Share

Thanks, your method helped. Additional check helped. Works from another class.

Only the question for me is not closed. I would like for myself to understand why, without additional verification, the creation of an instantiate behaves differently? If it is called directly on a line, then it is always created by one object, if through another class using FindObjectOfType, multiplication of objects is obtained. Although they do the same thing.

If theoretically there are thoughts why, then I would like to hear.

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

155 People are following this question.

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

Related Questions

Any way to create a gamobject only if there is an empty space? 1 Answer

in-editor cloning of game objects 1 Answer

How to instantiate prefab,Instantiating prefab is not wokring in this script 0 Answers

Instantiate rigidbody, transform or gameobject? 1 Answer

Photon Instantiate once at startup? 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges