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 /
  • Help Room /
avatar image
-1
Question by xyHeat · Mar 28, 2016 at 02:20 PM · gameobjectinstantiatequaterniontower-defense

Instantiate differents objects [C#]

Hi everybody ! I'm creating a tower defense game and i want to make a "tower selector". But I have a question : Can i make something like :

GameObject tower = (GameObject) tower1; GameObject turret = (GameObject) Instantiate (tower 1, ...)

Or must i make this code for all my towers with an if statement like that :

if(towerSelect 1){ GameObject turret = (GameObject) Instantiate (tower 1, ...) } if (......

Is there an other way ? Does arrays work for this ?

Thank you for your answers ! Bye, xyHeat

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 Geometrical · Mar 28, 2016 at 02:27 PM 0
Share

I've also been working on a sick tower defense game recently. If you want we can work together to make a game and as a benefit you'll be able to see my highly complex and efficient professional code.

Add me on Skype if you're interested: thomason.jane4

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by murkantilism · Mar 28, 2016 at 02:34 PM

To answer your first question: No, you cannot pass multiple tower objects to the Instantiate() function, it simply doesn't support that. I highly suggest you read the documentation on the function before attempting to use it, because Instantiate can be an expensive thing if done at a high volume over a small number of frames.

To answer your second question: there sure is. Like others have suggested, you can have a public List or GameObject[ ] array, and assign the towers you want to instantiate via the Inspector. Then a simple for or foreach loop to instantiate everything in said List or Array would be it!

I don't see how all of this would create a "tower selector" though, it sounds like you want to just highlight an existing tower, not spawn a new one. You should clarify what you're trying to do so we can better help ya.

Comment
Add comment · Show 3 · 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 xyHeat · Mar 28, 2016 at 03:23 PM 0
Share

Thank you for your answer ! I think that i will use an array and use tower[i] to select them !

In my $$anonymous$$d, my "tower selector" is just a button for each tower (with a picture, name, description, price, ...) and when you click on it, a public void define which tower is selected....

So i don't really know how I can reach this goal ...

Thank you again for your answer ! Bye, xyHeat

avatar image murkantilism xyHeat · Mar 28, 2016 at 03:47 PM 0
Share

You had me until: "when you click on it, a public void define which tower is selected"

What you said doesn't mean anything sensible...public is just an accessibility modifier for a variable or function, void is a return type. It sounds like your program$$anonymous$$g fundamentals are weak, I hope you are making this game as a learning experience.

Nonetheless I understand what you're trying to say. $$anonymous$$y approach would be to have each button call a separate function that instantiates the corresponding tower. If you aren't familiar with how the Unity Canvas system works, watch this first.

The button that creates a "Freeze Tower" should have an event OnClick() that would call a function in your script called (for example) SpawnFreezeTower(). This function would instantiate only the Freeze Tower object, not the entire array of gameObjects. Then your newly instantiated object would have to follow the mouse cursor (do that by converting screen to world coordinates) until the player clicks to place it. Somewhere you'd have to check to make sure they are placing it in a valid area (not on top of pre-existing towers, for example) before locking the object in position.

avatar image xyHeat murkantilism · Mar 28, 2016 at 04:27 PM 0
Share

Sorry, my sentence was wrong, yes i have a lot of difficulties to program !

Thank you for the button, i didn't thing this like that, i thought the game like this (i made this with one tower) :

you click on a quad, if there isn't already a tower, a tower is created and set a bool to say that a tower is already here. It's all, but i will try like you said, I think that can help me !

Thank you again for your answers ! Bye, xyHeat

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

51 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

Related Questions

Trying to get objects to instantiate and move at different angles 1 Answer

Having trouble with Instantiate 0 Answers

instantiate prefab at every gameobject with tag 0 Answers

More problems with arrow shooting 0 Answers

Problem with destroying a gameobject 2 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