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 /
avatar image
0
Question by MrPapillon · Jul 10, 2018 at 09:36 AM · instantiatearrayprefabsfindgameobjectswithtag

Instantiate specific object from "FindGameObjectsWithTag" Array

So, Im trying to instantiate some objects to my canvas, somethis like this: alt text

I made two scripts. The first is located into my scene objects and is so simple:

 public class Objects : MonoBehaviour
 {
     public GameObject _prefab;
 }

And the other one is on an emty object inside my canvas:

     public GameObject[] _objects;
     GameObject _player;
     [SerializeField]
     GameObject _cameraUI;
 
     bool _IsCreated;
 
     void Start()
     {
         _objects = GameObject.FindGameObjectsWithTag("Special_Objects");
         _player = GameObject.Find("FPSController");
 
         _cameraUI.SetActive(false);
         _IsCreated = false;
     }
 
     void Update()
     {
         foreach (GameObject _specialObject in _objects)
         {
             if (Vector3.Distance(_player.transform.position, _specialObject.transform.position) <= 2 && Input.GetKeyDown(KeyCode.E) && !_IsCreated)
             {
                 GameObject specialObject = Instantiate(_specialObject.GetComponent<Objects>()._prefab, transform.position, _specialObject.GetComponent<Objects>().transform.rotation);
                 specialObject.transform.localScale = new Vector3(10, 10, 10);
 
                 _player.SetActive(false);
                 _cameraUI.SetActive(true);
 
                 _IsCreated = true;
             }
 
             else if (Input.GetKeyDown(KeyCode.R) && _IsCreated)
             {
                 _player.SetActive(true);
                 _cameraUI.SetActive(false);
 
                 _IsCreated = false;
             }
         }
     }
 }

But there's something wrong. This is what is happening: alt text

What I want is just when player is close to an especific object, the code instantiate his respective prefab but I dont know how to do that.

unity-2018-07-10-11-26-56-38.jpg (83.5 kB)
unity-2018-07-10-11-26-40-62.jpg (79.6 kB)
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 tormentoarmagedoom · Jul 10, 2018 at 09:48 AM 0
Share

Good day.

I see you instantiate a Prefab stored in a script in another component. I never tried this, but i supose i should work.

Whats the problem ? Is the execution instantiating something? not? Are you sure the code reaches that line? Is instantiating something diferent? or at diferent position? or what?

Tell us whats the exatcly problem, what are you sure is working, what you dont know, what you need.. etc.. thx

avatar image MrPapillon tormentoarmagedoom · Jul 10, 2018 at 10:24 AM 0
Share

Yeah I think my code is instantiating but all the objects, not just the specific object that I want to instantiate. As you can see on the second picture, there are two objects instantiated. I want to pick one object, instantiate his prefab and when input "R" disable this.

0 Replies

· Add your reply
  • Sort: 

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

122 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

Related Questions

finding clones and destroying them doesn't work 0 Answers

How can I instantiate gameobjects from an array in a specific order? 1 Answer

How to learn amount of objects in selected prefabs folder? 0 Answers

Instantiating prefab at child (spawnlocations are arrays) 2 Answers

How to Instantiate a GameObject from a ScriptableObject piece of script? 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