Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Mitrovic_Z · Dec 10, 2021 at 08:44 PM · scripting problemunity 2deditor-scripting

How do I make an object spawn on another object's coordinates

Hi! I solved my other question from a couple minutes ago, my objects now spawn! New problem though, I wish to make them spawn on where they are supposed to go. For example here is my code atm:

public class Puzzle : MonoBehaviour { private List _listeTraps;

 [SerializeField] private GameObject[] _tableauTraps;
 [SerializeField] private GameObject[] _Spikes;
 [SerializeField] private GameObject[] _Mines;
 [SerializeField] private GameObject[] _Nothing;




 // Start is called before the first frame update
 void Start()
 {
     _listeTraps = new List<GameObject[]>();
     _listeTraps.Add(_Spikes);
     _listeTraps.Add(_Mines);
     _listeTraps.Add(_Nothing);
     Init();
 }

 private void Init(){
     for(int i = 0; i < _tableauTraps.Length; i++)
     {
         
         int _aleatoireSorte = Random.Range(0, _listeTraps.Count); //pige un chiffre random
         GameObject[] n = _listeTraps[_aleatoireSorte]; //n = au chiffre piger
         int _aleatoireTrap = Random.Range(0, n.Length);
         _tableauTraps[i] = n[_aleatoireTrap];
         Instantiate(n[_aleatoireTrap], _tableauTraps[i].transform.position, Quaternion.Euler(0,0,0));
         // _listeTraps.RemoveAt(_aleatoireSorte);
     }
    
 }

}

As of now, my object spawns kind of randomly, I want it to spawn on one of the elements in my _tableauTraps list. How do I do so?

Here's an image of my editor: alt text

screenshot.png (160.6 kB)
Comment
Add comment · Show 6
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 Mitrovic_Z · Dec 10, 2021 at 08:50 PM 0
Share

Hi! Forgot to mention, that I plan on having multiple traps generated and not just one!

avatar image Hellium · Dec 10, 2021 at 09:00 PM 0
Share

What do you mean by my object spawns kind of randomly? The code looks correct, you're providing the position of the spawn point as second argument of the Instantiate method, so unless the objects you instantiate have children which are not centered, I think your code works.

avatar image Mitrovic_Z Hellium · Dec 10, 2021 at 09:13 PM 0
Share

Whenever it spawns, it doesn't go to the right position, I want it to go on my element trap's position on my editor.

avatar image Mitrovic_Z Hellium · Dec 10, 2021 at 09:15 PM 0
Share

for example, the grey spot is where I want my trap to spawn, the black spot is where my trap is spawningalt text

capture2.png (99.3 kB)
avatar image Hellium Mitrovic_Z · Dec 10, 2021 at 09:24 PM 0
Share

Are you sure your trap object referenced in the _tableauTraps has a local position of (0, 0, 0)? (i.e is placed at the Traps's position?

You could try changing your code for:

      Instantiate(n[_aleatoireTrap]).transform.position = _tableauTraps[i].transform.position;

but I believe you'll get the same outcome.


P.S: Piece of advice: pick either english or french for your code, not a mix of both

Show more comments

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

249 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 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

Alphanumeric Sorting and GetChild not working!! 1 Answer

Make Object move a distance from a center. 1 Answer

How to deactivate an object in array which was chosen randomly before then activate randomly another one also in that array? 1 Answer

How can i add a second camera that will be showing only specific gameobject in game window ? 1 Answer

Unable to make 2D enemy zigzag : Top Down View 1 Answer


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