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 Hippie125 · Mar 08, 2012 at 08:34 AM · prefabplayerspawnpoint

Player spawn after touching

Help, im trying to make my player spawn when the game starts and when he dies from touching a killzone tag. Yes this may not be the most efficient way to do by if you want you can fix it up, im getting 3 errors on line 26: 1. 'expecting ), found : ' 2. ';' expected. Insert a semicolon at the end 3. Unexpected token: ).

Plz help i need this ASAP, and i have no idea what is wrong, i don't even know how to use arrays but anything would be helpful, it is a merge of two scripts one detecting weather you have hit a killzone and the spawn script. i have made shore that there are both java script Sorry for my bad spelling

 private var timer : float = 0.0;
 private var spawning : boolean = false;
 var prefab : GameObject;
 var numspawns : float = 0;
 var spawn1 : Transform;
 var spawn2 : Transform;
 var spawn3 : Transform;
 var spawn4 : Transform;
 var spawn5 : Transform;
 var spawn6 : Transform;
 var spawn7 : Transform;
 var spawn8 : Transform;
 var spawn9 : Transform;
 var spawn10 : Transform;
 var spawn11 : Transform;
 var spawn12 : Transform;
  
 function OnControllerColliderHit(hit : ControllerColliderHit)
 { 
     if(hit.gameObject.tag == "Killzone")
     {  
         Spawn();
     }
     else
     {
         OnControllerColliderHit(hit : ControllerColliderHit);
     }
 }
  
 function Spawn()
 {
  //set spawning to true, to stop timer counting in the Update function
  spawning = true;
  //reset the timer to 0 so process can start over
  timer = 0;
  
  //select a random number, inside a maths function absolute command to ensure it is a whole number
  var randomPick : int = Mathf.Abs(Random.Range(1,numspawns));
  
  //create a location 'Transform' type variable to store one of 3 possible locations declared at top of script
  var location : Transform;
  
  //check what randomPick is, and select one of the 12 locations, based on that number
  if(randomPick == 1){
   location = spawn1;
   Debug.Log("Chose pos 1");
  }
  else if(randomPick == 2){
   location = spawn2;
   Debug.Log("Chose pos 2");
  }
  else if(randomPick == 3){
   location = spawn3;
   Debug.Log("Chose pos 3");
  }
   else if(randomPick == 4){
   location = spawn4;
   Debug.Log("Chose pos 4");
  }
   else if(randomPick == 5){
   location = spawn5;
   Debug.Log("Chose pos 5");
  }
   else if(randomPick == 6){
   location = spawn6;
   Debug.Log("Chose pos 6");
  }
   else if(randomPick == 7){
   location = spawn7;
   Debug.Log("Chose pos 7");
  }
   else if(randomPick == 8){
   location = spawn8;
   Debug.Log("Chose pos 8");
  }
   else if(randomPick == 9){
   location = spawn9;
   Debug.Log("Chose pos 9");
  }
   else if(randomPick == 10){
   location = spawn10;
   Debug.Log("Chose pos 10");
  }
   else if(randomPick == 11){
   location = spawn11;
   Debug.Log("Chose pos 11");
  }
   else if(randomPick == 12){
   location = spawn12;
   Debug.Log("Chose pos 12");
  }
  //create the object at point of the location variable
  var thingToMake : GameObject = Instantiate(prefab, location.position, location.rotation);
   thingToMake.AddForce(Vector3(0,0,100));
  
  //halt script for 1 second before returning to the start of the process
  yield WaitForSeconds(1);
  //set spawning back to false so timer may start again
  spawning = false;
 }
Comment
Add comment · Show 4
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 Hippie125 · Mar 08, 2012 at 08:32 AM 0
Share

i dont know what has hapened to the code

avatar image Meltdown · Mar 08, 2012 at 08:40 AM 0
Share

Edit your post and mark the code with the 101010 icon on the toolbar.

avatar image Hippie125 · Mar 08, 2012 at 09:03 AM 0
Share

you can now edit the post, i cant get the code thing to work

avatar image FishBone · Mar 08, 2012 at 09:28 AM 1
Share

First: your OnControllerColliderHit calls itselfs if hit.gameObject.tag is not equal to "$$anonymous$$illzone", making an Infinite loop.

Second: $$anonymous$$athf.Abs() won't return an integer, it will only return a positive float, Random.Range() will return an integer if both arguments is integers, so changing your numspawns-variable to an integer and removing the $$anonymous$$ath.Abs will fix it (your variable 'location' has no values as the randomPick isn't integer)

It would be alot easier to know what your compiling errors are for if you would comment on what line is nr 26. Also it would be alot easier to do the locations with an array, you could just write "location = spawn[randomPick];" ins$$anonymous$$d of all the if checks :)

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kleptomaniac · Mar 08, 2012 at 02:18 PM

I think this code is in desperate need of a bit of tidying up and simplifying. Give @FishBone props for this one because he hit the nail right on the head in all respects.

 private var timer : float = 0.0;
 private var spawning : boolean = false;
 var prefab : GameObject;
 var spawns : Transform[];
 
 function OnControllerColliderHit(hit : ControllerColliderHit)
 { 
     if(hit.gameObject.tag == "Killzone")
     {  
         Spawn();
     }
 }
 
 function Spawn()
 {
     //Timer and spawning vars are not being used - Residual vars from previous script? - Left them in anyway
     
     //set spawning to true, to stop timer counting in the Update function
     spawning = true;
     //reset the timer to 0 so process can start over
     timer = 0;
 
     //Select a random number (from a range of ints)
     var randomPick : int = Random.Range(0, spawns.Length);
 
     //Instead of a Transform, you want a Vector3 here
     var location : Vector3;
 
      //Check what randomPick is, and select one of the 12 locations, based on that number
     location = spawns[randomPick].transform.position;
     Debug.Log("Chose pos " + randomPick);
 
     //create the object at point of the location variable
     var thingToMake : GameObject = Instantiate(prefab, location, Quaternion.identity);
       thingToMake.AddForce(Vector3(0,0,100));
 
      //halt script for 1 second before returning to the start of the process
      yield WaitForSeconds(1);
      
      //Again, left spawning in
      //set spawning back to false so timer may start again
     spawning = false;
 }

There you go, that should work! Again, remember to give FishBone an upvote for this one!

Klep

Comment
Add comment · Show 2 · 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 FishBone · Mar 08, 2012 at 02:55 PM 1
Share

Changing to an array you should also change the randompick to include 0. Also would be better to use spawns.Length ins$$anonymous$$d of numspawns to not get an error if you don't make 12 spawn-points.

So it should be:

var randomPick : int = Random.Range(0,spawns.Length);

avatar image Kleptomaniac · Mar 10, 2012 at 02:05 AM 0
Share

Indeed. I edited to show this. Thanks @FishBone!

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

2D Player Spawn Issue 0 Answers

How to turn a spawned enemy ( AI ) target on player?/ Why are my Hierarchy object turn prefab then no longer hold the transform object? 1 Answer

How to spawn prefab from a single cache when using multiple caches? 0 Answers

Spawning objects help 2 Answers

How to make a game with never ending waves of zombies like in COD zombies? 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