Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Block placing and destroying problems javascript

Discussion in 'Scripting' started by Boonearth, May 2, 2015.

  1. Boonearth

    Boonearth

    Joined:
    May 1, 2015
    Posts:
    20
    Recently i found a block placing and block destroying script i needed for my game.
    It was used in unity3d version 4 but now its not working when i updated to version 5.
    Here is the script also take note this is in javascript.
    If you can help me please post here im having trouble destroying blocks i can place them just fine.
    I just have trouble destroying them.

    savedscreen.png

    #pragma strict

    var grassBlock : GameObject;
    var dirtBlock : GameObject;
    private var selectedBlock : GameObject;

    function Start()
    {
    selectedBlock = dirtBlock;
    }

    function Update()
    {
    var hit : RaycastHit2D = Physics2D.Raycast(GetComponent.<Camera>().ScreenToWorldPoint(Input.mousePosition), Vector3.forward);

    if (Input.GetMouseButtonDown(0))
    {
    if (hit)
    {
    if (hit.collider.gameObject.tag == "block")
    {
    Destroy(hit.collider.gameObject);
    }
    }
    else if (!hit)
    {
    var instanceBlock : GameObject;
    instanceBlock = Instantiate(selectedBlock, GetComponent.<Camera>().ScreenToWorldPoint(Input.mousePosition) + Vector3(0,0,10), Quaternion.identity);
    instanceBlock.transform.position.x = Mathf.Round(instanceBlock.transform.position.x);
    instanceBlock.transform.position.y = Mathf.Round(instanceBlock.transform.position.y);
    }
    else
    {
    return;
    }
    }
    }
     
  2. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Add a debug line to make sure you're actually hitting something and we'll go from there...
    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var grassBlock : GameObject;
    4. var dirtBlock : GameObject;
    5. private var selectedBlock : GameObject;
    6.  
    7. function Start()
    8. {
    9.     selectedBlock = dirtBlock;
    10. }
    11.  
    12. function Update()
    13. {
    14.     var hit : RaycastHit2D = Physics2D.Raycast(GetComponent.<Camera>().ScreenToWorldPoint(Input.mousePosition), Vector3.forward);
    15.  
    16.     if (Input.GetMouseButtonDown(0))
    17.     {
    18.         if (hit)
    19.         {
    20.             Debug.Log(gameObject.name + " has hit " + hit.collider.gameObject.name + "!");
    21.             if (hit.collider.gameObject.tag == "block")
    22.             {
    23.                 Destroy(hit.collider.gameObject);
    24.             }
    25.         }
    26.         else if (!hit)
    27.         {
    28.             var instanceBlock : GameObject;
    29.             instanceBlock = Instantiate(selectedBlock, GetComponent.<Camera>().ScreenToWorldPoint(Input.mousePosition) + Vector3(0,0,10), Quaternion.identity);
    30.             instanceBlock.transform.position.x = Mathf.Round(instanceBlock.transform.position.x);
    31.             instanceBlock.transform.position.y = Mathf.Round(instanceBlock.transform.position.y);
    32.         }
    33.         else
    34.         {
    35.             return;
    36.         }
    37.     }
    38. }
     
  3. Boonearth

    Boonearth

    Joined:
    May 1, 2015
    Posts:
    20
  4. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    The reason I suggest this is because your code looks, at first glance anyway, like it should work fine. Granted, I've been up waaay too long haha. Let me know how it goes :)
     
  5. Boonearth

    Boonearth

    Joined:
    May 1, 2015
    Posts:
    20
    Look what happen Screenshot (7).png
     
  6. Boonearth

    Boonearth

    Joined:
    May 1, 2015
    Posts:
    20
    i put hierarchyType instead of hit.collider.gameObject so i fixed it but i still cant destroy blocks still can place them though.
     

    Attached Files:

  7. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    OK, it took a little digging and testing, but I've got it working on my side anyway... Try this:

    Code (JavaScript):
    1.     #pragma strict
    2.    
    3.     var grassBlock : GameObject;
    4.     var dirtBlock : GameObject;
    5.     private var selectedBlock : GameObject;
    6.    
    7.     function Start()
    8.     {
    9.         selectedBlock = dirtBlock;
    10.     }
    11.    
    12.     function Update()
    13.     {
    14.         var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
    15.    
    16.         if (Input.GetMouseButtonDown(0))
    17.         {
    18.             if (hit)
    19.             {
    20.                 Debug.Log(gameObject.name + " has hit " + hit.collider.gameObject.name + "!");
    21.                 if (hit.collider.gameObject.tag == "block")
    22.                 {
    23.                     Destroy(hit.collider.gameObject);
    24.                 }
    25.             }
    26.             else if (!hit)
    27.             {
    28.                 var instanceBlock : GameObject;
    29.                 instanceBlock = Instantiate(selectedBlock, GetComponent.<Camera>().ScreenToWorldPoint(Input.mousePosition) + Vector3(0,0,10), Quaternion.identity);
    30.                 instanceBlock.transform.position.x = Mathf.Round(instanceBlock.transform.position.x);
    31.                 instanceBlock.transform.position.y = Mathf.Round(instanceBlock.transform.position.y);
    32.             }
    33.             else
    34.             {
    35.                 return;
    36.             }
    37.         }
    38.     }
    39.  
     
  8. Boonearth

    Boonearth

    Joined:
    May 1, 2015
    Posts:
    20
    Its not letting me destroy the blocks ill post a link soon of a video i just made so you can see what iam talking about.
     
  9. Boonearth

    Boonearth

    Joined:
    May 1, 2015
    Posts:
    20
    here is the link to the video i just created showing that its not working to destroy the block i have no errors with the script its just not wanting to destroy the blocks
     
  10. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    OK, you're still using the old code... try the new code I just gave ya.
    Nevermind, lol, should have watched further ;) Lemme finish...
     
  11. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Alright, give me a screenshot of the Inspector with one of your blocks selected please. Either it's not tagged correctly, or something else is amiss.
     
  12. Boonearth

    Boonearth

    Joined:
    May 1, 2015
    Posts:
    20
    Here you go Screenshot (9).png
     
  13. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Alright...

    1) It looks like this block is still using the old script, not the newer one (switch it from block.js to blockM2.js).

    2) The script is looking for blocks with the tag "block", but yours are Untagged, so if you don't already have it setup, create a new tag named "block" and add that tag to your block prefabs.

    I've tested it further on my own and I'm happily creating and destroying blocks at will ;)
     
    Last edited: May 2, 2015
  14. Boonearth

    Boonearth

    Joined:
    May 1, 2015
    Posts:
    20
    Ok i have to go somwhere in about 30 mins i got the blocks to destroy and place but i cant place mutiple after i destroy one then i place one i cant place a second one. So i may not be doing it right. But i have to go and get ready thank you for your help. Ill be back later thank you. Skype if you have one Boonearth.Gaming
     
    krougeau likes this.
  15. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    No Skype, but I'll be happy to help further later, if you need. Have fun!
     
  16. Boonearth

    Boonearth

    Joined:
    May 1, 2015
    Posts:
    20
    Ok i have been having problems with not placing blocks now im destroying blocks but i cant seem to place any i used to be able to place multiple now i cant can you help me on that
     
  17. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Without having your entire project to examine, I would really just be guessing at what might be wrong... I was able to take the script above and use it to create and destroy blocks with no trouble. I didn't mess with trying to change block types or anything (and the code above is only really set up for placing "dirt" blocks as is & needs to be extended), but placing and destroying the dirt blocks has worked flawlessly on my end.

    Some things to check would be to:

    (1) make sure you have the correct script(s) attached (for instance, in your screenshot above, you were using the old and not the new script, plus it says that you have another script attached that needs to be fixed before it will work),

    (2) make sure you have your prefab blocks properly assigned to the script in the Inspector,

    (3) make sure your blocks are tagged as "block, and

    (4) check your Console output to see if you're getting any errors or other messages that might better help us to figure out what's going wrong.

    Let me know what you find and I'll help however I can. I have been pretty busy the last few days, so it may take me a bit, but I promise I'll respond as soon as I can :) Have fun!
     
  18. Boonearth

    Boonearth

    Joined:
    May 1, 2015
    Posts:
    20
    latest error i iam using the script you gave me and its still not working i updated the tags and prefabs Screenshot (10).png
     
  19. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    Sorry, I had assumed that you had a camera in your scene that was called Main Camera and that had the tag "MainCamera", as these are Unity's default settings for a new scene. The call to Camera.main is, I believe, looking specifically for these things to be set up. If you have things set up otherwise, then we'll have to adjust the script and have it find & access whatever the camera is in your scene...

    Click on your camera in the scene and take a screenshot of it's Inspector settings for me, then I can make the necessary adjustments and hopefully get you back on track.
     
  20. Boonearth

    Boonearth

    Joined:
    May 1, 2015
    Posts:
    20
    Ok here you go sorry i took a little break from it. Now here is the picture of the inspector.
     

    Attached Files:

  21. krougeau

    krougeau

    Joined:
    Jul 1, 2012
    Posts:
    451
    OK, it's been awhile and I no longer have all of the scripts to look through... Can you tell me where you found the scripts, or what tutorial(s) you were following? Your camera looks to be set up fine, but in retrospect, I see that the errors your receiving are likely because you've place the primary script on the wrong object(s). If you can give me a link to where the scripts came from, I can go over them again and give you proper instructions for making use of them. If you're not sure, then just post the block.js code here for me to reference (since we already have blockManager.js here in the thread).