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 murkertrer · Apr 22, 2014 at 08:08 PM · instantiatescenedestroycannonball

Instantiate a destoyed object? Or instantiate an Object that is not on the Scene?

Hey! sorry for the noob question, just getting started:

I have a canon that shoots cannonballs,when they hit something they explode.

1)In order to instantiate the cannonball, Do i have to have the cannonball somewhere in the scene?

If i do this, because the cannonball is triggered to explode on contact, as soon as it spawns, it explodes and I cannot instantiate it again.

Is it posible to instantiate an object that is not on the scene?

Help much much apreciated

 function HoldToShoot (){
     if (Input.GetButtonDown("Fire1")) {
          StartX = Time.time;
     }

     if (Input.GetButtonUp("Fire1")){
         EndX = Time.time;
         force = (EndX-StartX)*20;
         
   
         
         clone = Instantiate(clone, transform.position, transform.rotation);
              clone.velocity = transform.TransformDirection (Vector3.forward * force);
         }
         }
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
1
Best Answer

Answer by xortrox · Apr 22, 2014 at 08:19 PM

If you got the cannon ball in your scene view right now, drag and drop it into your project window/tab, this will create a prefab (a copy of your cannon ball), drag and drop this into a script with a public GameObject variable to hold it and use this variable when instantiating.

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

Answer by Fearthefrozt · Apr 23, 2014 at 05:31 PM

You're going to want to create a prefab of the cannon ball just like xortrox said. Here is a video on how to create them.

https://www.youtube.com/watch?v=HECGcwwRM64

You will then want to use the Instantiate() function to create a clone of the prefab in the scene whenever you shoot the cannon ball. This means when it's destroyed it simply destroys the clone and you can easily use Instantiate() to create another one. For information on this function look at:

http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html

I typically put my prefabs in a prefab folder in a Resources folder in Assets so that I can load it into the scene through code. Say you created a prefab called cannonball and after placing it in the prefabs folder the code would look something like this:

 Instantiate(Resources.Load("prefabs/cannonball"), new Vector3(0,0,0), Quaternion.identity);

That would instantiate the cannonball at position 0,0,0 in the world.

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

Answer by murkertrer · Apr 24, 2014 at 04:35 PM

Thank you guys!! XD

 function HoldToShoot (){
 
 if (ammoCount > 0){
     if (Input.GetButtonDown("Fire1")) {
          StartX = Time.time;
          ammoCount-=1;
     }
     if (Input.GetButtonUp("Fire1")&& Time.time > nextFire){
         nextFire = Time.time + fireRate;     
         
         EndX = Time.time;
         force = (EndX-StartX)*20;
         if (force>45){
             force=45;}
             shootingForce.text ="Max45.Force:" + force;         
 
 var clone : Rigidbody;
 clone = Instantiate(regular, transform.position, transform.rotation); 
 
 clone.velocity = ransform.TransformDirection(Vector3.forward * force);
 }}}    
Comment
Add comment · 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

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

22 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Extra objects keep appearing in scene view 0 Answers

Destroy All Objects In The Scene??? 1 Answer

how to destroy camera instatiated from prefab? 0 Answers

Accessing children of an instance doesn't work every time 1 Answer

Object Instantiate Image to Canvas, Object destroy, Image destroy? 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