Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Jetzz · Jul 17, 2014 at 10:33 PM · pickuppaper

Cant pickup papers with script

I cant pickup papers with script , with "E" , and i dont know why.

 #pragma strict
 @script RequireComponent( AudioSource )
 
 var papers : int = 0;
 var papersToWin : int = 8;
 var distanceToPaper : float = 2.5;
 
 public var paperPickup : AudioClip;
 
 var theEnemy : EnemyScript;
 
 
 
 
 function Start() 
 {
     Screen.lockCursor = true;
     
     // find and store a reference to the enemy script (to reduce distance after each paper is collected)
     if ( theEnemy == null )
     {
         theEnemy = GameObject.Find( "Enemy" ).GetComponent( EnemyScript );
     }
 }
 
 
 function Update() 
 { 
     //if ( Input.GetMouseButtonUp(0) ) // use E in editor as LockCursor breaks with left mouseclick
     if ( Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.E) ) 
     {
         //var ray = Camera.main.ScreenPointToRay( Input.mousePosition ); // always cast ray from center of screen
         var ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0.0 ) );
         var hit : RaycastHit;
         if ( Physics.Raycast( ray, hit, distanceToPaper ) )
         {
             //if ( hit.collider.gameObject.tag == "Paper" )
             if ( hit.collider.gameObject.name == "Paper" )
             {
                 papers += 1;
                 //Debug.Log( "A paper was picked up. Total papers = " + papers );
                 
                 audio.PlayClipAtPoint( paperPickup, transform.position ); 
                 
                 Destroy( hit.collider.gameObject );
                 
                 // make enemy follow closer
                 theEnemy.ReduceDistance();
             }
         }
     }
 }
 
 
 function OnGUI()
 {
     if ( papers < papersToWin )
     {
         GUI.Box( Rect( (Screen.width * 0.5) - 60, 10, 120, 25 ), "" + papers.ToString() + " Papers Collected" );
     }
     else
     {
         GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 35 ), "All Papers Collected!" );
         // Application.LoadLevel( "sceneWin" );
     }
 }
Comment
Add comment · Show 24
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 Kiwasi · Jul 17, 2014 at 10:40 PM 2
Share

Nice to see some originality. Normally the picking up papers script requires 5 to win...

avatar image ExplodingCookie · Jul 17, 2014 at 11:05 PM 1
Share

I don't see anything wrong with the code... $$anonymous$$ight be missing something though. Do you have any errors in the console?

avatar image Kiwasi · Jul 17, 2014 at 11:27 PM 2
Share

Put debug.log everywhere. You need to check if your input is working, if your raycast is returning true and if your collider name check is working.

BTW, tag checking is normally preferred over name checking.

avatar image AlucardJay · Jul 18, 2014 at 02:54 AM 1
Share

ARE YOUR OBJECTS NA$$anonymous$$ED PAPER ??!!

  • http://www.youtube.com/watch?v=-XIaWwe$$anonymous$$wH4

  • http://www.youtube.com/watch?v=JaOsHCV_SZQ

if I had a dollar for everytime....

avatar image AlucardJay · Jul 18, 2014 at 03:08 AM 1
Share

Add this at line 37 :

 Debug.Log( "Raycast hit " + hit.collider.gameObject.name );

This will print in the console if/what your raycast hit.

and also try increasing the length of the raycast (change distanceToPaper to a higher number).

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ExplodingCookie · Jul 17, 2014 at 10:50 PM

,To answer your question. Try using this in place of your reference to the key.

 Input.GetKeyDown("e")

Hopefully this solves your issue. :)

~ExplodingCookie

Comment
Add comment · Show 4 · 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 Jetzz · Jul 17, 2014 at 10:54 PM 0
Share

Its the same. :(

avatar image ExplodingCookie · Jul 17, 2014 at 10:58 PM 1
Share

I'm aware they will do the same thing in theory. But using this always works for me. The best way to solve a problem is to tinker around with things.

avatar image ExplodingCookie · Jul 17, 2014 at 11:00 PM 1
Share

Have you tried restarting Unity?

avatar image Jetzz · Jul 18, 2014 at 01:14 AM 0
Share

Yes i restart but nothing

avatar image
0

Answer by Kiwasi · Jul 18, 2014 at 03:10 AM

Here is a debugged version of your update function. You should see all of the messages in the console. I suggest commenting each one out once you have seen it, messaging the console will slow the game down.

This will allow you to isolate which line is not working. Post back once you have figured it out.

 function Update() 
 { 
     Debug.Log("Update is working");
     if ( Input.GetMouseButtonDown(0) || Input.GetKeyDown(KeyCode.E) ) 
     {
         Debug.Log("Inut is working");
         var ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0.0 ) );
         var hit : RaycastHit;
         if ( Physics.Raycast( ray, hit, distanceToPaper ) )
         {
             Debug.Log("Raycast is working");
             if ( hit.collider.gameObject.name == "Paper" )
             {
                 Debug.Log("Name checking is working");
                 papers += 1;
                 audio.PlayClipAtPoint( paperPickup, transform.position ); 
                 Destroy( hit.collider.gameObject );
                 theEnemy.ReduceDistance();
             }
         }
     }
 }
  


Comment
Add comment · Show 5 · 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 Jetzz · Jul 18, 2014 at 03:13 AM 0
Share

Update is working , and Inut is working

avatar image Kiwasi · Jul 18, 2014 at 03:23 AM 1
Share

Now we are getting somewhere. The problem is in your raycast. Let me have a look at that a little more closely and get back to you.

avatar image Kiwasi · Jul 18, 2014 at 03:35 AM 1
Share

Try adding the following line immediately after your var ray.

 Debug.DrawRay(ray.origin, ray.direction * distanceToPaper, Color.white, 1, false);

This should draw the line in your scene view. Is this line co$$anonymous$$g anywhere near your paper collider?

avatar image Jetzz · Jul 18, 2014 at 07:04 PM 0
Share

Now the code is this:

 #pragma strict
 @script RequireComponent( AudioSource )
 
 var papers : int = 0;
 var papersToWin : int = 3;
 var distanceToPaper : float = 5.5;
 
 public var paperPickup : AudioClip;
 
 var theEnemy : EnemyScript;
 
 
 
 
 function Start() 
 {
     Screen.lockCursor = true;
     
     // find and store a reference to the enemy script (to reduce distance after each paper is collected)
     if ( theEnemy == null )
     {
         theEnemy = GameObject.Find( "Enemy" ).GetComponent( EnemyScript );
     }
 }
 
 
  
 function Update() 
 { 
     Debug.Log("Update is working");
     if ( Input.Get$$anonymous$$ouseButtonDown(0) || Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E) ) 
     {
         Debug.Log("Inut is working");
         var ray = Camera.main.ScreenPointToRay( Vector3( Screen.width * 0.5, Screen.height * 0.5, 0.0 ) );
         Debug.DrawRay(ray.origin, ray.direction * distanceToPaper, Color.white, 1, false);
         var hit : RaycastHit;
         if ( Physics.Raycast( ray, hit, distanceToPaper ) )
         {
             Debug.Log("Raycast is working");
             if ( hit.collider.gameObject.name == "Paper" )
             {
                 Debug.Log("Name checking is working");
                 papers += 1;
                 audio.PlayClipAtPoint( paperPickup, transform.position ); 
                 Destroy( hit.collider.gameObject );
                 theEnemy.ReduceDistance();
             }
         }
     }
 }
 
 
 function OnGUI()
 {
     if ( papers < papersToWin )
     {
         GUI.Box( Rect( (Screen.width * 0.5) - 60, 10, 120, 25 ), "" + papers.ToString() + " Papers Collected" );
     }
     else
     {
         GUI.Box( Rect( (Screen.width/2)-100, 10, 200, 35 ), "All Papers Collected!" );
         // Application.LoadLevel( "sceneWin" );
     }
 }

The debug doesnt make any line

avatar image Jetzz · Jul 18, 2014 at 08:22 PM 0
Share

I think the problem is here: if ( Physics.Raycast( ray, hit, distanceToPaper ) )

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

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

Related Questions

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

Two exact objects behaving differently... *scratches head* 0 Answers

ComputeShader version of Physics.Raycast 4 Answers

Simple question about OnMouseButtonAsUp 1 Answer

Animated Texture Offset 1 Answer


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