• 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 RyanJ · Mar 10, 2014 at 01:43 AM · javascriptinstantiateprefabkeypressrelative

Instantiate a prefab with a key press to a relative location?

I'm making a 2D infinite runner game. I've created a prefab called Block that I want to spawn into the level whenever I press the X key. I want it to spawn at 15, 1.5, 0, which is 15 relative to the Player since they are always moving forward and 1.5 relative to the ground since the player will be jumping and I don't want the blocks to spawn in the air.

Below is the javascript I have right now, which creates the prefab at 0,0,0. I don't know how to deal with the problem of the Player always running and jumping. The script is attached to the main camera, which is attached to the player; if that is important. I'm still a beginner, so I'd appreciate as much detail as possible, and thanks!

 #pragma strict
 
 var objectName : GameObject;
 function Start()
 {
    
     
 }
 
 function Update()
 {
 if(Input.GetKeyDown(KeyCode.X)) {
 var pos ; Vector3 (0,0,0); 
      var rot : Quaternion = Quaternion.identity; 
      Instantiate(objectName);
 }
 }
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

1 Reply

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

Answer by getyour411 · Mar 10, 2014 at 01:59 AM

 pos : Vector3(player.transform.position.x+15, player.transform.position.y+1.5,0);
 
 Instantiate(objectName, pos, rot);
Comment
Add comment · Show 5 · 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 RyanJ · Mar 10, 2014 at 04:32 AM 0
Share

I added this in where I thought I am supposed to, but I keep getting errors no matter what I change.

 #pragma strict
 
 var objectName : GameObject;
 var player : GameObject;
 function Start()
 {
    
     
 }
 
 function Update()
 {
 if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X)) {
 pos : Vector3(player.transform.position.x+15,player.transform.position.y+1.5,0);
      var rot : Quaternion = Quaternion.identity; 
      Instantiate(objectName, pos, rot);
 }
 }

It's telling me that pos is an unexpected token. I added the variable "player", because I was getting some other error. Also, what is "player" in "player.transform.position" a reference to? Is that where the name of the player is supposed to be? Because the name of the player gameobject is Character and is tagged as Player. Sorry, I'm just bad at this.

avatar image getyour411 · Mar 10, 2014 at 04:37 AM 0
Share

$$anonymous$$ake sure you have dragged your player into the player field in Inspector; the name or tag is irrelevant. I don't do JS but maybe you need the keyword 'var' in front of the pos = ... bit

avatar image RyanJ · Mar 10, 2014 at 04:55 AM 0
Share
 if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X)) {
 var pos : Vector3;((player.transform.position.x+15) (player.transform.position.y+1.5,0));
      var rot : Quaternion = Quaternion.identity; // Quaternion.identity essentially means "no rotation"
      Instantiate(objectName, pos, rot);
 }
 }

I added var and then it said I needed a semi colon after Vector3 and then it didn't like the comma and lack of parenthesis. So, this is what it looks like now and it's saying " It is not possible to invoke an expression of type 'float'". Hmmm it's getting late so I'm gonna have to sleep on this, but what did you mean by "dragged your player into the player field in Inspector"?

avatar image getyour411 · Mar 10, 2014 at 10:00 PM 0
Share

You put a semi-colon after Vector3, remove that; you put parenthesis around the Vector3 arguments, undo all that; I can't really $$anonymous$$ch you JS syntax line by line in this space, some basics are assumed/needed.

Regarding player, you setup a variable named player when you added the line var player : GameObject;

In the Inspector, if you look at the GameObject this script is attached to you'll see a field named player; drag your Player (from the Project hierarchy/windows next to scene) into that field. Again, that's pretty much Unity Day 1 app basics, you might really benefit from a few tutorials - I wish you luck.

avatar image RyanJ · Mar 10, 2014 at 11:15 PM 0
Share

Finally got it, thanks a ton!

 #pragma strict
  
 var objectName : GameObject;
 var player : GameObject;
 function Start()
 {
  
  
 }
  
 function Update()
 {
 if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.X)) {
 var pos = Vector2(15,2); 
      var rot : Quaternion = Quaternion.identity; 
      Instantiate(objectName,pos+player.transform.position, rot);
 }
 }

Link to my other question with same problem: http://answers.unity3d.com/questions/660701/instantiate-to-relative-location.html

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

Need help understanding scripted prefab behavior - when I click one, the script runs on ALL prefab instances, not just the one I clicked. 3 Answers

Problems instantiating a prefab (javascript) 1 Answer

Unity Frame Drop From Instantiating Prefab that has script 0 Answers

Instantiate to relative location? 2 Answers

[Urgent]Changing variables of an object AFTER instantiation 2 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