Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
This question was closed Jul 30, 2018 at 09:55 AM by Mega_Jimmy for the following reason:

Answer was not exactly correct but it definitely helped me look at the right things.

avatar image
0
Question by Mega_Jimmy · Jul 29, 2018 at 10:53 PM · jumpinginvoke

Jump Button needs to do two things

I'm trying to develop my game with consoles in mind, so buttons often serve multiple purposes, either to keep things simple for the player or due to input limitations on standard controllers. With that in mind I want to use one button to Jump and Talk. The talking side works fine;

 if (Input.GetKeyDown(KeyCode.Space) && grounded == true)
         {
             if (hit.collider.tag.Equals("NPC"))
             {
                 if (talking == true && DiaCount == 0){
                     hit.collider.GetComponent<DialogueTrigger>().Invoke("TriggerDialogue", 0f);
 
                 } else if (DiaCount > 0) {
                     pin.GetComponent<DialogueManager>().Invoke("DisplayNextSentence", 0f);
                 }
             }
             else if (hit.collider.tag.Equals(null))
             {
                 rb.velocity = Vector3.up * jumpForce;
             }

When I invoke the speech version of this (with the text boxes and whatnot) it works pretty much like a charm. When I try to invoke the Jump however, it comes up with "object set to null reference" as an error. The idea is that I don't want the player to jump when talking with an NPC, but I do at pretty much any other time so long as they're on the ground.

Anybody got any ideas on what I can do with this? I tried taking out the if (hit.collider.tag.Equals(null)) to see if that made any difference but it comes back with the same error.

I appreciate any assistance!

Comment
Add comment · Show 1
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 Carterryan1990 · Jul 31, 2018 at 12:35 AM 0
Share

your right i over looked that, was helping a few others and kinda rushed, not to mention i cant actually test the code, but you live and you learn =]. I thought about it a little today and came up with this solution which i think may be a little simpler, but thats for you to decide.

  if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && grounded == true)
         {
             if (talking)
             {
                 if (hit.collider.tag.Equals("NPC"))
                 {
                     if (DiaCount == 0)
                     {
                         hit.collider.GetComponent<DialogueTrigger>().Invoke("TriggerDialogue", 0f);
 
                     }
                     else if (DiaCount > 0)
                     {
                         pin.GetComponent<Dialogue$$anonymous$$anager>().Invoke("DisplayNextSentence", 0f);
                     }
                 }
             }
             else
             {
                 rb.velocity = Vector3.up * jumpForce;              
             }
         }

 


1 Reply

  • Sort: 
avatar image
0

Answer by Stratosome · Jul 30, 2018 at 12:01 AM

Hi there!

I'm not quite sure exactly what is causing the error. I'm guessing nothing was actually "hit", so you are trying to grab the collider and tag of null? But you said you removed that line and it still happened, so I dunno. I'd have to see more code to know exactly what you've got the error.


However, give something like this a shot. Whenever you press space, create a circle and check if there are interactable things nearby. If so, interact obviously, if not, jump. The code here is written for 2D, but if you're wanting 3D, I'm pretty sure the same would work, you'd just need to change a couple things. Let me know if this helps!

 if (Input.GetKeyDown(KeyCode.Space)) {
 
     bool canInteract = false;
     Collider2D interactableCollider = null;
     Collider2D[] colliders = Physics2D.OverlapCircleAll(transform.position, interactRadius);
 
     // Checks if there is something that can be interacted with nearby
     foreach (Collider2D col in colliders) {
         if (col.tag.Equals("NPC")) {
             canInteract = true;
             interactableCollider = col;
         }
     }
 
     if (canInteract) {
         if (talking == true && DiaCount == 0) {
             interactableCollider.GetComponent<DialogueTrigger>().Invoke("TriggerDialogue", 0f);
         }
         else if (DiaCount > 0) {
             // I don't know what pin is but yeah
             pin.GetComponent<DialogueManager>().Invoke("DisplayNextSentence", 0f);
         }
     }
     else {
         rb.velocity = Vector3.up * jumpForce;
     }
 
 }
Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

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

How to prevent steep wall climbing? 2 Answers

how can i change the x position while jumping on air using c#? 1 Answer

Advanced jumping 0 Answers

Why is my object jumping to 0,0,0 coordinates? 0 Answers

Detecting CharacterController jumping 0 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