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 Makenshi · Feb 26, 2011 at 11:33 PM · javascriptinstantiatedestroytime

destroying an object instantiated that came out of spawn point

hi i have a problem here i want to destroy an object that comes out of an spawn point im trying to do 3 different spawn points and so they can throw different objects from them that will hit the player the spawn is fine and everything but as i said i cant make them dissapear if i use destroy(gameobject) it will destroy the spawner and well that is not good. in any case heres my code

var timeToWait : float = 1; var timeToWait2 : float = 4; private var timer : float = 0; private var timer2 : float = 0; var beginSpawn : boolean = false; var prefab : Transform; private var spawnPoint : Vector3; function Update() { if(beginSpawn) { spawnPoint = this.transform.position; timer += 1*Time.deltaTime; timer2 += 1*Time.deltaTime; if(timer >= timeToWait) { prefab =Instantiate (prefab, spawnPoint, this.transform.rotation); timer =0; }

         prefab.transform.position.z -=0.1;
     if(timer2 >= timeToWait2)
     {       
     //Destroy(GameObject.FindWithTag("Clone"));
     timer2 =0;
     }       
 }

}

to make it easier what i want to do is destroy the things that come out of the spawner after a certain time and thats about all thank you (oh yeah this code is inside my spawners and i want to use Transform for this tyvm)

if you cant understand what im trying to do tell me and i will try to explain >.<

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

3 Replies

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

Answer by AngryOldMan · Feb 27, 2011 at 04:31 AM

Okay im not sure exactly what your after but it sounds like you could benefit from a little bit more information. In this is example lets say we have a game with a cannon that fires a cannonball every 5 seconds, the player does not control this object or its actions.

In your scene First create a cylinder gameobject. This will be our cannon.Next create a new javascript. Name it something like "CannonScript". In the project window drag the CannonScript onto the Cannon in the hierarchy window. Next (with the CannonScript selected) go to "edit" in the inspector window this will open your script editor of choice. In the script paste this code. Make sure you read and understand it!

//this is where we will specify the cannonball prefab in the inspector
var CannonballPrefab : Rigidbody;
//this is when the next shot will be instantiated, it goes up everytime the function succesfully plays through
var nextShotTime : float = 4.0;
//this is the time inbetween each instantiate, you can edit this time in the inspector to make the time between shots longer
var timeBetweenShots : float = 4.0;
//this is how fast your cannonball will be fired, change this in the inspector depending on how big your level is
var CannonballSpeed : float = 2;
//change this depending on how big your cylinder is,this is for if it's scaled by 1
var offset : float = 1.6;
//the actions in this function are performed every frame
function Update()
{
    //exectued when the variable "nextShotTime" is less than the time passed in the game
    if (nextShotTime < Time.time)
    {
        //this first line adds the "timeBetweenShots" variable to the "nextShotTime" variable
        nextShotTime = Time.time + timeBetweenShots;
        //this is where you instantiate the CannonballPrefab which you should have set, for the script, in the inspector
        //this line states that the CannonballPrefab will be instantiated on the "up/Y" axis of the Cannon, the offset is so that it comes out of the end.
        var Cannonball = Instantiate(CannonballPrefab, transform.position + transform.up*offset, transform.rotation);
        //this line gives the Cannonball, which we have instatiated, a direction and speed to go.
        Cannonball.rigidbody.velocity = transform.up * CannonballSpeed;
        //this is a debug log, you can see if the script has worked as a message will pop up in the bottom right corner of the Unity editor
        Debug.Log ("Cannon has just fired");
    }
}

Next you need a cannonball so create a sphere in the scene,call it Cannonball, and with it highlighted in the hierarchy give it a rigidbody (Component ~> Physics ~> Rigidbody) Now create a new prefab in the project window and drag the Cannonball from the hierarchy window onto the prefab in the project window. With the Cannon selected, drag the newly made Cannonball prefab into the approriate slot under the Cannon's CannonScript component. Then create a new javascript to be attached to the Cannonball. This script is for destroying the Cannonball whenever it collides with something, for example your player or the ground.

function OnCollisionEnter(collision : Collision) { Destroy(gameObject);

}

Press play and you should now see the Cannon firing the Cannonball(Clone) in whatever direction the Y axis is pointing. If not check the debug log is there. If the debug log is not there check for error messages. If the debug log is there then adjust the Cannon's variables in the inspector while the game is running (during runtime) until you can see it. Once you can see the Cannonballs then you can start playing around with the different elements like shot speed and the mass and gravity on the rigidbody. You can also create 3 prefabs of the Cannon, the same way you made a prefab of the cannonball, and put a different type of cannonball and different shot settings on each cannon prefab.

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 Makenshi · Feb 27, 2011 at 05:22 PM 0
Share

yeah i think im doing that but what i dont get is if you are telling the object to get destroyed i mean the cannonball then how can you still keep on producing them if they are not anymore on the scene because i need them to get destroyed on time and not on collision

avatar image Makenshi · Feb 27, 2011 at 05:29 PM 0
Share

oh wait so u can make the prefabs like that ok ty this solved the question tyvm

avatar image AngryOldMan · Feb 27, 2011 at 08:07 PM 0
Share

put the script "tool55" suggested on your cannonball prefab and that should give you the effect you want

avatar image
0

Answer by AngryOldMan · Feb 27, 2011 at 12:02 AM

put the destory object script on the prefab of what ever it is your instantiating. So something like

function Awake
{
  yield WaitForSeconds (4);
  Destroy (GameObject);
}

WARNING this is just sample code you will have to assign your own variables and such.

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 Makenshi · Feb 27, 2011 at 12:15 AM 0
Share

this wont work because i dont know how to make an object from something that doesnt exists and that will delete the parent object and i wont be able to make copies of something that doesnt exists :S but ty

avatar image tool55 · Feb 27, 2011 at 03:10 AM 0
Share

This should work but you don't need yield WaitForSeconds(). The Destroy function has a timer built in. Couldn't be easier. See below.

avatar image
0

Answer by tool55 · Feb 27, 2011 at 03:09 AM

Your spawned objects are prefabs. Just attach the script to the prefab, then every instance will have the script attached.

var destructTime: float;

function Awake() { Destroy (gameObject, destructTime) }

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 Makenshi · Feb 27, 2011 at 05:23 PM 0
Share

yeah i tried that but as i said in the top comment when i destroy the object i can no longer instantiate more of them since the object no longer exists there

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

No one has followed this question yet.

Related Questions

Instantiate prefabs just before it comes into view 2 Answers

Instantiate JS error that i can't figure out 1 Answer

Instantiate prefabs before it comes into view 0 Answers

removing an instantiated prefab 1 Answer

How to make something happen over time? 1 Answer


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