• 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
1
Question by buildman99 · Feb 11, 2012 at 03:16 PM · javascriptinstantiatevariablesblock

multiple instantiate object script help

im making a block based building game and im making it so when you press 1 and left click it places block 1, press 2 block2 etc. so i dont know how to script it. heres my script.

CODE:

 var blockLayer : LayerMask = 1;
 var range : float = Mathf.Infinity;
 var hit : RaycastHit;
 var block1 : Transform;
 var block2 : Transform;
 var block3 : Transform;
 var block4 : Transform;
 var whichblock = 1;
 
 function Block4 ()
 {
     if (Input.GetKeyDown(4))
         var whichblock = 4;
 }
 
 function Block3 ()
 {
     if (Input.GetKeyDown(3))
         var whichblock = 3;
 }
 
 function Block2 ()
 {
     if (Input.GetKeyDown(2))
         var whichblock = 2;
 }
 
 function Block1 ()
 {
     if (Input.GetKeyDown(1))
         var whichblock = 1;
 }
 
 function Update () {
         if (Input.GetMouseButtonDown(0))
         {
         if (whichblock) == 1;
         Build1(); 
         if (whichblock) == 2;
         Build2();
         if (whichblock) == 3;
         Build3();
         if (whichblock) == 4;
         Build4();
         }
     
     if (Input.GetMouseButtonDown(1))
         Erase();
 }
 
 function Build1() {
     if (HitBlock()) {
         Instantiate (block1);
         block1.transform.position = hit.transform.position + hit.normal;
         }
     }
     
 function Build2() {
     if (HitBlock()) {
         Instantiate (block2);
         block2.transform.position = hit.transform.position + hit.normal;
         }
     }
 
 function Build3() {
     if (HitBlock()) {
         Instantiate (block3);
         block3.transform.position = hit.transform.position + hit.normal;
         }
     }
     
 function Build4() {
     if (HitBlock()) {
         Instantiate (block4);
         block4.transform.position = hit.transform.position + hit.normal;
         }
     }
 
 
 function Erase() {
     if (HitBlock())
         Destroy(hit.transform.gameObject);
 }
 
 function HitBlock() : boolean {
     return Physics.Raycast(transform.position, transform.forward, hit, range, blockLayer);
 } 
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
0

Answer by aldonaletto · Feb 11, 2012 at 03:38 PM

The functions Block1 to Block4 were not being called anywhere - but they would not work anyway, because the var keyword declared a new and temporary whichblock variable in each function: temporary variables are deleted on exit, and the original whichblock would never be modified.
I would change the first script part like this:

var blockLayer : LayerMask = 1; var range : float = Mathf.Infinity; var hit : RaycastHit; var block1 : Transform; var block2 : Transform; var block3 : Transform; var block4 : Transform; var whichblock = 1;

function Update () { // check the pressed keys in Update! if (Input.GetKeyDown("1")) whichblock = 1; if (Input.GetKeyDown("2")) whichblock = 2; if (Input.GetKeyDown("3")) whichblock = 3; if (Input.GetKeyDown("4")) whichblock = 4; if (Input.GetMouseButtonDown(0)){ switch (whichblock){ // switch is easier to use case 1: Build1(); break; case 2: Build2(); break; case 3: Build3(); break; case 4: Build4(); break; } } if (Input.GetMouseButtonDown(1)) Erase(); }

// the rest of the script remains the same

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 buildman99 · Feb 11, 2012 at 03:58 PM

thank you for the answer i see what i did wrong!

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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Creating a Loadout 1 Answer

How can i make an automatic world generator? 1 Answer

Speed Pill... Variable referencing and adjusting temporarily 3 Answers

Instantiate prefab within parameters?(Javascript) 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