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 SHIMMY · Jan 20, 2013 at 10:05 PM · prefabinstancerespawnsnake

How to respawn an object again after it has been instantiated

This is for a snake game, and i have looked everywhere, and spent days doing it so this really is my final resort.

I am using two prefabs, one for a body to add to the snake, and another one for food. I have it set up so that at the start, the food randomly respawns, and after collision does create the instance of the body prefab. However, i need it to respawn again after collision, and it simply doesn't. Only the once. How do i get to respawn again and again?

 //Bodyprefab
 var BodyPrefab : Transform; 
 
 // food prefab
 public var pickupPrefab:GameObject;
  
 // the spawnpoint to be spawned out
 private var spawnPoint:GameObject;
  
 
 //awake functions are called before the script starts
 function Awake()
 {
     // retrieve GameObject tagged as 'SpawnPoint' within the 'PickupSpawnPoints' GameObject which this script is a Component of
     spawnPoint = gameObject.FindWithTag("SpawnPoint");
     
    // spawn the pickup
     SpawnPickup();
 }   
     
 function OnControllerColliderHit (collision : ControllerColliderHit) 
 {
     if(collision.gameObject.tag == "Player")
         {
           print ("FOOD!");
           Instantiate(BodyPrefab, GameObject.Find("Snake").transform.position, Quaternion.identity);
 //originally it did say "Destroy(gameObject)" but i assumed that was destroying the food prefab, with or without it didn't make a difference.
           SpawnPickup();
           }
 }
 
 
     function SpawnPickup()
 {
     // retrieve the position and rotation of the pickup's spawn point
     var spawnedPickupPosition = spawnPoint.transform.position; // copy position
 
     spawnedPickupPosition.z = Random.Range (-11.7,11.8);
 
     spawnedPickupPosition.x = Random.Range(11.5,-11.9);
     
     var spawnedPickupRotation:Quaternion = spawnPoint.transform.rotation;
      
     // instantiate (create) the pickup prefab with the above position and rotation
     var spawnedPickup:GameObject = Instantiate(pickupPrefab, spawnedPickupPosition, spawnedPickupRotation);
      
     // set the spawned pickup as a child of the 'PickupSpawnPoints' gameobject that this script is a Component of
     spawnedPickup.transform.parent = spawnPoint.transform;
 }
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 SHIMMY · Jan 22, 2013 at 11:10 AM 0
Share

please answer =[

avatar image AlucardJay · Jan 22, 2013 at 11:49 AM 1
Share

However, i need it to respawn again after collision, and it simply doesn't. Only the once. How do i get to respawn again and again?

As in Epic by Faith No $$anonymous$$ore .... What is IT ?

It is hard to understand. So, snake hits Food, food gets destroyed, snake gets a body part, food is instantiated somewhere else randomly. This part I get.

Are you saying that more food is not instantiated, or when you collide with food no extra body part is instantiated?

avatar image SHIMMY · Jan 22, 2013 at 12:29 PM 0
Share

No more food is instantiated, when i collide with food body part is instantiated. But because no more food spawns, neither can any more body parts.

avatar image AlucardJay · Jan 22, 2013 at 03:50 PM 0
Share

Start off with some basic debugging to see what is going on in the collider function, eg :

 function OnControllerColliderHit (collision : ControllerColliderHit) 
 {
     Debug.Log( "Controller Hit : " + collision.gameObject.tag )
     if(collision.gameObject.tag == "Player")
     // etc

this should display in the console the tag of each collision, from here you should be able to see if the tag is Player, or something else that is stopping this from working.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by 3Dx2Yz · Jan 22, 2013 at 12:11 PM

  spawnPoint = gameObject.FindWithTag("SpawnPoint");

It seems you have a simple error in your code.

If you write "gameObject" you refer to your current GameObject this script it assigned on. If you want to find another Game Object from your Project, you need to write:

 spawnPoint = GameObject.FindWithTag("SpawnPoint");
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 Meater6 · Jan 22, 2013 at 12:15 PM 1
Share

Ooh, I missed that. Good eyes. :)

avatar image SHIMMY · Jan 22, 2013 at 03:36 PM 0
Share

No, sorry it still hasn't worked, same as before

avatar image 3Dx2Yz · Jan 22, 2013 at 05:57 PM 0
Share

$$anonymous$$aybe try to change this line: var BodyPrefab : Transform; into this line: var BodyPrefab : GameObject;

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

12 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

Related Questions

Changing the Variables of An Instanced Script 2 Answers

How to refer to a collision between clones of the same prefab? C# 1 Answer

Updating a variable on a script in an instanced object 1 Answer

How to instantiate different prefab on the end of a chain of prefabs? 2 Answers

Breaking a prefab doesn't break it completely? 5 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