• 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 FatalNightmare · Dec 16, 2012 at 07:32 PM · javascriptgameobjectspawnprefabspoint

How do you Place/Spawn a GameObject or Prefab in front of Player

Hello, I am making a game where Player can pick up "walls" just like picking up coins or points. I am trying to have it so if the player has say "2 walls" than he can hit the '1' or 'e' keys and a wall GameObject or Prefab will appear in front of the character. I need help with the code I don't know where to start or anything. Here is the code for my Wall pickup (This code is on the Player)

 var PlayerScore : int;
 var ScoreText = "Walls: 0";
 var MaxPoints : int;
 var gText : GUIText; 
 
 function OnTriggerEnter(other : Collider){
     if(other.tag == "Point"){
        PlayerScore += 1;
        ScoreText = "Walls: " + PlayerScore;
        Destroy(other.gameObject);
     }
 }
 
 function Update(){
     if(PlayerScore > MaxPoints){
        PlayerScore = 0;
        print("MaxReached");
        ScoreText = "Walls: 0";
     }
     gText.text = ScoreText; 
 }



Please help me with the code for placing the GameObject in front of the Player. Do I need to make a new JavaScript or addon to this Wall one. If it is a new Javascript where do I add it to? Thank you for your time, I would appreciate any help I can get.

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

2 Replies

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

Answer by clunk47 · Dec 16, 2012 at 10:58 PM

Here. Use this as a separate script just for testing. Attach this UnityScript to your player. Drag a prefab or other object onto the slot for (Prefab Object) in the inspector. When you press 'E', it will instantiate that object 3 units(default) in front of the player. You can take this and tweak it to your needs, and add what you need from it to your script. You can enable and disable the boolean "CanPlaceObject" via your player's "Wall Score". Something like

 if(PlayerScore > 0)
     canPlaceObject = true;
 else if(PlayerScore <= 0)
     canPlaceObject = false;

That's just an example. Here is my script.

     public var prefabObject : GameObject;
     var canPlaceObject : boolean = true;
     private var offset : Vector3;
     private var fwd : Vector3;
     private var clone : GameObject;
     var unit : float = 3.0f;
     
     function Update()
     {
         fwd = transform.TransformDirection(Vector3.forward) * unit;
         offset = transform.position + fwd;
         if(Input.GetKeyDown(KeyCode.E) && canPlaceObject)
         {
             clone = Instantiate(prefabObject, offset, transform.rotation);
         }
     }
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 FatalNightmare · Dec 16, 2012 at 11:27 PM 1
Share

Very helpful Script but I am still confused on the if statement. Would I put it on this new script or my Wall Pickup script?

avatar image clunk47 · Dec 16, 2012 at 11:29 PM 1
Share

You would attach this script to you Player.

avatar image clunk47 · Dec 16, 2012 at 11:29 PM 1
Share

So if you're wanting to take pieces from this script, yes, you would copy from this script and paste into the script you had originally attached to your player.

avatar image
0

Answer by strachpr01 · Dec 16, 2012 at 10:14 PM

you can add the following to this script

 private var playerPos:Vector3;
 private var spawnPos:Vector3;
 public var player: GameObject;
 public var objectToInstantiate: GameObject;
 
 function DoInstatiate(){
 playerPos=player.transform.position;
 spawnPos= Vector3(playerPos.x+10, playerPos.y, playerPos.z); ///change as needed
 Instantiate (objectToInstantiate, spawnPos, Quaternion.identity);
 }

then just call this function when you wish to create the object

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 FatalNightmare · Dec 16, 2012 at 10:24 PM 0
Share

What do you mean by call this function. How do I call that function? Put it in another function that activates when 'e' is pressed? What code do I need to put so that the above code is called when 'e' is pressed?

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Slender pages collection does not activate gameobject I want 0 Answers

How to instantiate multiple Prefabs to multiple Childs randomly ? 2 Answers

When GameObject is spawned, rotate x 90. 1 Answer

Generate random number and set a GameObject active. 1 Answer

Spawn random amount of gameobjects 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