• 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 BananaClipStudio · Jun 13, 2012 at 09:41 AM · instantiatespawn

Spawning Ammo

Okay I have tried to research this using both the scripting reference and on Unity Answers but I guess I am having troubles understanding the Instantiate function with my random ammo spawning script.

I am wanting to spawn a random ammo kit at a specific spawn point. I am setting up the variables or instantiate right? I am a compete noob when it comes to scripting.

 var ammo = 0;

var pistolAmmo = GameObject;

var rifleAmmo = GameObject;

var shotgunAmmo = GameObject;

var assaultAmmo = GameObject; var x = 0;

var y = 0;

var z = 0;

function Start () { AmmoSpawn(); }

function AmmoSpawn() {

 ammo = Random.Range(1, 4);
 
 switch(ammo)
 {
 
     case 1: //Spawns Pistol Ammo
         
          Instantiate (pistolAmmo, Vector3(x, y, z), Quaternion.identity);
         
         break;
     
     case 2: //Spawns Rifle Ammo
         
         Instantiate(rifleAmmo, Vector3(x, y, z), Quaternion.identity);
     
         break;
     
     case 3: //Spawns shotgun Ammo
         
         Instantiate(shotgunAmmo, Vector3(x, y, z), Quaternion.identity);
     
         break;
     
     default: //Spawns assault Rifle Ammo
         
         Instantiate(assaultAmmo, Vector3(x, y, z), Quaternion.identity);
     
         
 
 }



}

Comment
Add comment
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

2 Replies

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

Answer by Dissolver84 · Jun 13, 2012 at 02:24 PM

You could try do this by spawning at certain place. You might need to create new tags by going into tag manager.

 var ammoBox : Transform;
 var spawnpoint : Transform;
 
 function Update () {
 //Here, the if statement means if the spawnpoint has a "empty" tag
 // It will Instantiate an ammo box where the spawnpoint is
 if (spawnpoint.CompareTag("Empty")) {
 Instantiate(ammoBox, spawnpoint.transform, spawnpoint.rotation);
 spawnpoint.Tag = ("used");
 }
 //If the spawnpoint has a "used" tag
 //It will not do anything or you could put some actions
 else if (spawnpoint.CompareTag("used")) {
 //do nothing or you could choose
 }
 }
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 BananaClipStudio · Jun 13, 2012 at 03:22 PM 0
Share

I am trying to just spawn ammo for a random weapon when a player gets close to the spawnpoint. I got things fixed by not using a Vector3 and just using transforms. But I will be using your snippet for another script that I have to make. :)

avatar image
0

Answer by phren · Jun 13, 2012 at 02:41 PM

In your script you have errors in the declarations of your ammo box types e.g. "var pistolAmmo = GameObject;". This should generate a syntax error. If you want to specify the type of a variable use the syntax "var pistolAmmo : GameObject;" instead (use : instead of =). After that be sure to set pistolAmmo etc. inside the editor to their proper values.

Other than that your script should work as intended unless I've missed something. :P

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 BananaClipStudio · Jun 13, 2012 at 03:20 PM 0
Share

yeah I had placed a random int just to fill each variable and later changed it to GameObjects and just forgot to change the operator. Thanks

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Issue With Spawning Enemies (javascript) 2 Answers

Hide/Freeze something? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

No appropriate version of 'UnityEngine.Object.Instantiate' 0 Answers

Spawn Prefab or FBX model after import? 1 Answer

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