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
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
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.
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
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.
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
Follow this Question
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