- Home /
Answer was not exactly correct but it definitely helped me look at the right things.
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!
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;
}
}
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;
}
}
Follow this Question
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