- Home /
Drilling-game script's logical bug.
function Update() {
if(Input.GetButton("Down") && CanBreak){
DrillDown();
CanBreak = false;
Debug.Log("Pressed Down");
}
}
function DrillDown(){
Debug.Log("DrillDown");
var blockName : String = "GridBlock" + parseInt(Mathf.Round(player.transform.position.x)) + "-" + parseInt(Mathf.Round(player.transform.position.y - 0.5));
var block : GameObject = GameObject.Find(blockName);
if(block == null){
Debug.Log(blockName + " doesn't exist!!");
}else{
yield WaitForSeconds(BreakDelay);
if(Input.GetButton("Down")){
if(block.tag == "Dirt"){
Debug.Log("Dirt +1");
GameObject.Destroy(block);
Debug.Log("Destroyed");
CanBreak = true;
}
if(block.tag == "Stone"){
Debug.Log("Stone +1");
GameObject.Destroy(block);
CanBreak = true;
}
}else{
CanBreak = true;
}
}
}
Hello, I have a problem with my script. When I press down, it drills a block but it doesn't set CanBreak to true. It just won't let my drill another block. I know it has something to do with the boolean CanBreak. My debug log says this:
I just don't get why it does that. Thank you -Nelis
You should just call Destroy(block) not GameObject.Destroy(block).
I still get the same error, unfortunately. I get the error at line 87, that's where if(block.tag == "Dirt").
hi have you tried removing yield wait for seconds and you could also use raycast right
That error means it cant find an object with the tag "Dirt", simply it doesnt exist. Are you destroying it too early?
Answer by deltamish · Apr 04, 2013 at 07:25 AM
Hi here is the script It casts a ray downwards and checks the tag
Edit Working
public var CanBreak:boolean = true;
var range:float = 1;
function Update(){
if(Input.GetButton("Down") && CanBreak){
CanBreak = false;
Drill();
}
if(!Input.GetButton("Down")){
CanBreak = true;
}
}
function Drill(){
var hit:RaycastHit;
if(Physics.Raycast(transform.position,-Vector3.up,hit,range)){
if(hit.collider.tag == "Dirt"){
Destroy(hit.collider.gameObject);
CanBreak = true;
}
if(hit.collider.tag == "Stone"){
Destroy(hit.collider.gameObject);
CanBreak = true;
}
if(!hit.collider){
CanBreak = true;
}
}
}
Note hit should be typed(`Physics.Raycast(transform.position,-Vector3.up,hit <-- tells what type of ray to be casted , if not typed it wont hit any object at all ,which in turn returns null,range))`before range else it will only cast a ray and wont know what type of ray
I get this error now: NullReferenceException: Object reference not set to an instance of an object $$anonymous$$apGrid.Drill () (at Assets/Scripts/$$anonymous$$apGrid.js:88) $$anonymous$$apGrid.Update () (at Assets/Scripts/$$anonymous$$apGrid.js:63)
btw, already thank you for all your help and effort! -Nelis
Hi sorry about that i had mistyped _Vector3.up ins$$anonymous$$d of typing -Vector3.up
I'm sorry, but I'm still getting the same error:NullReferenceException: Object reference not set to an instance of an object $$anonymous$$apGrid.Drill () (at Assets/Scripts/$$anonymous$$apGrid.js:88) $$anonymous$$apGrid.Update () (at Assets/Scripts/$$anonymous$$apGrid.js:63)
The error at line 88 is where I call the Drill() function in Update. Ther error at line 63 is where I check if hit.collider.tag == "Dirt".
$$anonymous$$mm are you sure you tagged the objects Dirt or Rock Note if the tag is other than Dirt or Rock it will return null
hit.gameObject
is missing because Raycasthit
doesn't have gameObject
. you can use hit.collider.gameObject
ins$$anonymous$$d
Answer by EliteMossy · Apr 02, 2013 at 11:02 AM
The problem is probably this:
function Update() {
if(Input.GetButton("Down") && CanBreak){
DrillDown();
CanBreak = false;
Debug.Log("Pressed Down");
}
}
it should be
function Update() {
if(Input.GetButton("Down") && CanBreak){
CanBreak = false;
DrillDown();
Debug.Log("Pressed Down");
}
}
You simply had it the bool being set to false in the wrong place, meaning it will always be false after the first block is drilled.
Answer by Murtaza-Learner · Apr 02, 2013 at 12:50 PM
Are you calling DrillDown() somewhere else also? If not then I guess problem is with canBreak = false
in the if condition when you are calling in Update function. You assign canBreak = true
in DrillDown() and again make it false when the control returns back to the Update function. Solution : I guess if you make canBreak = false
in the DrillDown() and canBreak = true
in the Update() outside the if
will do the stuff ok. Try it out.
It works now but I'm getting this error: $$anonymous$$issingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. Your script should either check if it is null or you should not destroy the object. $$anonymous$$apGrid+$DrillDown$5+$.$$anonymous$$oveNext () (at Assets/Scripts/$$anonymous$$apGrid.js:87)