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 MrVopemz · Nov 07, 2012 at 12:19 AM · minecraft

Block Placing Script Help

I have a Minecraft like script for placing and destroying blocks. When I try to destroy a block the whole game crashes and I dont know why. I also dont know how to change the cube to a block prefab I have called Grass Block.

Script -

 var blockLayer : LayerMask = 1;
 var range : float = Mathf.Infinity;
 var hit : RaycastHit;
 
 function Update () {
     if (Input.GetMouseButtonDown(0))
         Build();
     if (Input.GetMouseButtonDown(1))
         Erase();
 }
 
 function Build() {
     if (HitBlock()) {
         var cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
         cube.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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by sparkzbarca · Nov 07, 2012 at 12:39 AM

So we got a few wierd things here. First off this physics cast to the blockLayer. You create a layer, you cast only to a layer, but there is nothing in that layer.

At what point did you assign the cubes you generate to the blockLayer?

I'd not worry about layers right now, just try to make it work without to start.

whats this attached to? Is there anything at all in the blocklayer?

do this insert a log to figure out what your trying to destroy. Perhaps its the player.

 function Erase(){
 if (hitblock())
 {
   Debug.log(hit.transform.gameObject);
 }

as to how to create a prefab this is in c# but its easy to transfer

this is my create object script

 public class CreateObject : MonoBehaviour {
     public GameObject temp;
     // Update is called once per frame
     void Update () {
         
         
     if (Input.GetKeyDown(KeyCode.Alpha1))
         {
             temp.rigidbody.isKinematic = true;
             temp.transform.position = Camera.main.ViewportToWorldPoint(new Vector3(.5f, .5f, 3));        
             Instantiate(temp);        
         }
 
     }
 }

notice i have a public gameobject called temp. that shows up in the inspector as something i can see when i have the script selected. I then drag my prefab over it and now temp is whatever prefab i dragged onto it.

i then modify the position of the object each time to make sure it will be created in the right spot and then use the keyword Instantiate to bring it into the game.

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 MrVopemz · Nov 07, 2012 at 12:54 AM 0
Share

@sparkzbarca Thanks for the help but this is JavaScript not C#

avatar image sparkzbarca · Nov 07, 2012 at 01:03 AM 0
Share

void becomes function other that that almost nothing changes. :P

well for this script

avatar image Raxoez · Mar 20, 2019 at 08:54 AM 0
Share

Well that C# script does work, but how to fix this problem? https://cdn.discordapp.com/attachments/395951953969938432/557849161018638337/unknown.png i did try modify the position, but nothing work

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

A node in a childnode? 1 Answer

How to expose variables in c# according to selected booleans 1 Answer

Why is my update function acting wierd? 1 Answer

open a link inside unity 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