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.
#pragma strict
var grassBlock : GameObject;
var dirtBlock : GameObject;
private var selectedBlock : GameObject;
function Start()
{
selectedBlock = dirtBlock;
}
function Update()
{
var hit : RaycastHit2D = Physics2D.Raycast(GetComponent.().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.().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;
}
}
}
Add a debug line to make sure you’re actually hitting something and we’ll go from there…
#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)
{
Debug.Log(gameObject.name + " has hit " + hit.collider.gameObject.name + "!");
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;
}
}
}
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
i put hierarchyType instead of hit.collider.gameObject so i fixed it but i still cant destroy blocks still can place them though.
OK, it took a little digging and testing, but I’ve got it working on my side anyway… Try this:
#pragma strict
var grassBlock : GameObject;
var dirtBlock : GameObject;
private var selectedBlock : GameObject;
function Start()
{
selectedBlock = dirtBlock;
}
function Update()
{
var hit : RaycastHit2D = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if (Input.GetMouseButtonDown(0))
{
if (hit)
{
Debug.Log(gameObject.name + " has hit " + hit.collider.gameObject.name + "!");
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;
}
}
}
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.
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
OK, you’re still using the old code… try the new code I just gave ya.
Nevermind, lol, should have watched further Lemme finish…
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.
Alright…
-
It looks like this block is still using the old script, not the newer one (switch it from block.js to blockM2.js).
-
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
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
1 Like
No Skype, but I’ll be happy to help further later, if you need. Have fun!
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
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!
latest error i iam using the script you gave me and its still not working i updated the tags and prefabs
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.
Ok here you go sorry i took a little break from it. Now here is the picture of the inspector.