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 davidflynn2 · Oct 10, 2012 at 08:28 PM · c#

Spawning portal

I have a prefab called portal and I want to make it so that when my player press the P button it makes a portal appear infront of them. I would like to make it so that it is a little infront of them that way you dont instantly go though it.

I have tried this but it never spawns my object.

 var prefab : Transform;
 
 if(Input.GetKey(KeyCode.D)) {
 Instantiate (prefab, Vector3(2.0, 0, 0));
 }
Comment
Add comment · Show 5
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 kmeboe · Oct 10, 2012 at 08:33 PM 0
Share

"prefab" should be a GameObject, like this:

var prefab : GameObject;

Then, in the inspector, drag your prefab into the "prefab" variable on your script.

avatar image Muuskii · Oct 10, 2012 at 09:22 PM 1
Share

I've gotten prefabs to work as Transforms, I'm not aware of a performance difference but it works for me :)

Where is that if condition stationed? It has to be in some kind of Update function. But also, the Vector3(2.0, 0, 0) is going to cause it to be 2 units to the right of the center of the world.

If your character is far from the center, you may be unaware that it's spawning.

avatar image AlucardJay · Oct 10, 2012 at 11:51 PM 1
Share

You can Instantiate into many different types :

 #pragma strict
 
 var theCubeT : Transform;
 var theCubeG : GameObject;
 var theCubeS : CubeScript;
 
 function Update () {
     if( Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T) ) 
     {
         Instantiate( theCubeT, transform.position + (transform.forward * 2.0), Quaternion.identity );
     }
     
     if( Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.G) ) 
     {
         Instantiate( theCubeG, transform.position + (transform.forward * 2.0), Quaternion.identity );
     }
     
     if( Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S) ) 
     {
         Instantiate( theCubeS, transform.position + (transform.forward * 2.0), Quaternion.identity );
     }
 }
avatar image kmeboe · Oct 10, 2012 at 11:57 PM 0
Share

Right, but isn't the type important? For instance, if he's spawning a portal, it's probably not a "Transform", but rather some type of GameObject.

Or does the type not matter for prefab variables?

avatar image AlucardJay · Oct 11, 2012 at 12:04 AM 0
Share

It really just depends on the kind of reference you want to keep and use in your script. If you are just checking position or manipulating position , then store and use a reference to the transform. If you are using the variables on that script, clone to the script type, you can still step up through the components to the transform (or gameObject).

From memory, transform is an external call ( gameObject.transform ) so it is better to store a local reference (cache) a transform . For this purpose, the clone is not instantiated into a variable or array, so the next Update the object is lost anyway =]

1 Reply

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

Answer by AlucardJay · Oct 10, 2012 at 11:57 PM

Ok, a couple of problems here. Check the instantiate command first :

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

 Instantiate (original : Object, position : Vector3, rotation : Quaternion) : Object 

it says you need to provide an object, a position and a rotation. The easiest way to do this is :

 Instantiate( original , transform.position, Quaternion.identity );

this will clone 'original' at the position and at a rotation of zero.

to make something be 2 units in front of something, use this formula :

 transform.position + (transform.forward * 2.0)

put it all together and you have :

 #pragma strict
 
 var prefab : Transform;
 
 function Update () {
     if( Input.GetKeyDown(KeyCode.D) ) 
     {
         Instantiate( prefab, transform.position + (transform.forward * 2.0), Quaternion.identity );
     }
 }

And as I said in my comment, you can Instantiate into many types =]

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

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

Multiple Cars not working 1 Answer

Please Help Convert js to c# 1 Answer

Procedural Generation 1 Answer

Distribute terrain in zones 3 Answers

PinBall Flippers 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