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 mineus64 · Jul 31, 2013 at 06:29 PM · inspectorblockplace

Block Placement Help

How do I modify this script so that instead of creating a standard cube, it instead creates a prefab, which can be specified from within the inspector? For example, instead of creating a cube when the button is clicked, it creates a building, or a tree, or a sherbet lemon.

     var distance = 5;
      
     function OnGUI ()
     {
     if (GUI.Button(Rect(25,25,100,30), "Place Block"))
     {
     var newCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
     newCube.transform.position = transform.position + transform.forward * distance;
     newCube.transform.rotation = transform.rotation;
     }
     }
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 chronicfail · Jul 31, 2013 at 06:36 PM

Try adding a variable at the top "var prefab : Transform" and replacing the whole newCube section with something like:

var newPrefab=Instantiate(prefab,transform.position+transform.forward*distance,transform.rotation);

That will set position and rotation of the prefab at the same time, also.

Comment
Add comment · Show 4 · 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 mineus64 · Jul 31, 2013 at 06:46 PM 0
Share

Thanks, it works. Although, I'm having a problem with my prefab spawning in the air, and although it has a rigidbody, it either hovers in the air or falls through the terrain, depending on whether I lock the y position or not.

avatar image chronicfail · Jul 31, 2013 at 06:55 PM 1
Share

That's wierd, but sounds more like a problem with your prefab. Does it have a collider attached and if it does, have you made sure that the "isTrigger" box is not selected? Also, if your prefab is using a mesh collider, that will not work with rigidbodies unless "convex" is selected.

avatar image mineus64 · Jul 31, 2013 at 06:59 PM 0
Share

Ok, thanks for the info. How would you add a downward transform to the newPrefab? Because if I could do that, then I could set it up so that it always is placed on the ground? $$anonymous$$aybe get the height of the spawned object above the ground, and transform it downwards that much?

avatar image chronicfail · Aug 01, 2013 at 08:01 AM 0
Share

If you put a script on the prefab with something like this:

 function Start () {
 
 var hit : RaycastHit;
 
 if(Physics.Raycast(transform.position,Vector3.up*-1,hit){
 var hitDistance=Vector3.Distance(transform.position,hit.point);
 transform.translate(0,-hitDistance,0);
 }

}

it would raycast directly downward, and, if there was an object underneath, move it down to the point where the raycast intersects with the object underneath.

avatar image
1

Answer by alexguerin · Jul 31, 2013 at 06:39 PM

Take a look at Instantiate.

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

17 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

Related Questions

Multiple Cars not working 1 Answer

In-World Placement Help 1 Answer

Quick Custom Inspector Question 1 Answer

Unity Networking, connecting to sever problems :( 1 Answer

What is wrong with this? 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