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 Nelis · Apr 02, 2013 at 10:22 AM · javascriptboolean

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: alt text

I just don't get why it does that. Thank you -Nelis

screen.png (338.5 kB)
Comment
Add comment · Show 19
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 SubatomicHero · Apr 02, 2013 at 03:48 PM 1
Share

You should just call Destroy(block) not GameObject.Destroy(block).

avatar image Nelis · Apr 02, 2013 at 03:54 PM 0
Share

I still get the same error, unfortunately. I get the error at line 87, that's where if(block.tag == "Dirt").

avatar image deltamish · Apr 02, 2013 at 04:06 PM 0
Share

hi have you tried removing yield wait for seconds and you could also use raycast right

avatar image SubatomicHero · Apr 02, 2013 at 04:51 PM 1
Share

ok...whats the error?

avatar image SubatomicHero · Apr 05, 2013 at 09:03 AM 1
Share

That error means it cant find an object with the tag "Dirt", simply it doesnt exist. Are you destroying it too early?

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 20 · 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 Nelis · Apr 04, 2013 at 01:12 PM 0
Share

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

avatar image deltamish · Apr 05, 2013 at 05:26 AM 0
Share

Hi sorry about that i had mistyped _Vector3.up ins$$anonymous$$d of typing -Vector3.up

avatar image Nelis · Apr 05, 2013 at 08:37 AM 0
Share

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".

avatar image deltamish · Apr 06, 2013 at 05:33 AM 0
Share

$$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

avatar image Chronos-L · Apr 09, 2013 at 10:01 AM 1
Share

hit.gameObject is missing because Raycasthit doesn't have gameObject. you can use hit.collider.gameObject ins$$anonymous$$d

Show more comments
avatar image
1

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.

Comment
Add comment · Show 1 · 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 Nelis · Apr 02, 2013 at 12:00 PM 0
Share

I have changed it, but it still doesn't work.

avatar image
0

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.

Comment
Add comment · Show 1 · 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 Nelis · Apr 02, 2013 at 01:21 PM 0
Share

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)

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

17 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

One use per level question 1 Answer

Two toggles, only one can be on, both can be off 3 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