Search Unity

Calling function Pickup with no parameters but the function requires 1

Discussion in 'Scripting' started by unity_96pants, Jun 29, 2019.

  1. unity_96pants

    unity_96pants

    Joined:
    Jun 29, 2019
    Posts:
    3
    I've been trying to fix this for over 2 hours and I really can't seem to figure out where the error is coming from.


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerInteract : MonoBehaviour
    6. {
    7.    
    8.  
    9.     public GameObject currentInterObject = null;
    10.    
    11.  
    12.     private void Update()
    13.     {
    14.         if(Input.GetButtonDown ("Interact") && currentInterObject)
    15.         {
    16.           currentInterObject.SendMessage("Pickup");
    17.         }
    18.     }
    19.  
    20.     void OnTriggerEnter2D(Collider2D other)
    21.     {
    22.         if (other.CompareTag("InterObject"))
    23.         {
    24.             currentInterObject = other.gameObject;
    25.         }
    26.     }
    27.  
    28.     void OnTriggerExit2D(Collider2D other)
    29.     {
    30.         if (other.CompareTag("InterObject"))
    31.         {
    32.             if (other.gameObject == currentInterObject)
    33.             {
    34.                 currentInterObject = null;
    35.             }
    36.         }
    37.     }
    38.  
    39.    
    40.  
    41. }
     
    Last edited: Jun 29, 2019
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Does the Pickup function take any arguments? You didn't post the source for Pickup.
     
  3. unity_96pants

    unity_96pants

    Joined:
    Jun 29, 2019
    Posts:
    3
    Here's the source for Pickup.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class InteractionObject : MonoBehaviour
    7. {
    8.  
    9.     public GameObject Effect;
    10.     private HUD health;
    11.     private PlayerStats stats;
    12.     private gameMaster gm;
    13.  
    14.     public int Heal = 1;
    15.  
    16.  
    17.     //Text
    18.     private void Start()
    19.     {
    20.         gm = GameObject.FindGameObjectWithTag("GameMaster").GetComponent<gameMaster>();
    21.     }
    22.  
    23.     void OnTriggerEnter2D(Collider2D other)
    24.     {
    25.         if (other.CompareTag("Player"))
    26.         {
    27.  
    28.             if (other.CompareTag("Player"))
    29.             {
    30.                 gm.InputText.text = ("[E] to pickup");
    31.             }
    32.         }
    33.     }
    34.  
    35.     void OnTriggerStay2D(Collider2D other)
    36.     {
    37.         if (other.CompareTag("Player"))
    38.         {
    39.            
    40.             if (other.CompareTag("Player"))
    41.             {
    42.                 if(Input.GetButtonDown("Interact"))
    43.                 {
    44.                     Pickup(other);
    45.                     gm.InputText.text = (" ");
    46.                 }
    47.             }
    48.         }
    49.     }
    50.  
    51.     void OnTriggerExit2D(Collider2D other)
    52.     {
    53.         if (other.CompareTag("Player"))
    54.         {
    55.             //Pickup(other);
    56.             if (other.CompareTag("Player"))
    57.             {
    58.                 gm.InputText.text = (" ");
    59.             }
    60.         }
    61.     }
    62.  
    63.     public void Pickup(Collider2D player)
    64.     {
    65.         Instantiate(Effect, transform.position, transform.rotation);
    66.  
    67.         GetComponent<SpriteRenderer>().enabled = false;
    68.         GetComponent<Collider2D>().enabled = false;
    69.  
    70.        
    71.         //Give Buff to Player
    72.         PlayerStats stats = player.GetComponent<PlayerStats>();
    73.         stats.curHealth = stats.curHealth + Heal;
    74.  
    75.  
    76.         //stats.curHeatlh = stats.maxHealth;
    77.  
    78.         // yield return new WaitForSeconds(duration);
    79.  
    80.         //stats.maxHealth /= multiplier;
    81.         //player.transform.localScale /= multiplier;
    82.  
    83.         //Item Pickup
    84.         Destroy(gameObject);
    85.        
    86.     }
    87.  
    88.    
    89. }
    90.  
     
  4. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    Pickup is expecting a Collider2D argument so you need to pass that in the sendMessage call.

    Example
    currentInterObject.SendMessage("Pickup", GetComponent<Collider2D>());
     
  5. unity_96pants

    unity_96pants

    Joined:
    Jun 29, 2019
    Posts:
    3
    So something like this might work then?

    Code (CSharp):
    1. if(Input.GetButtonDown ("Interact") && currentInterObject)
    2.         {
    3.           currentInterObject.SendMessage("Pickup", GameObject.FindGameObjectWithTag("Player").GetComponent<Collider2D>());
    4.         }
     
  6. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,735
    It looks like it ought to.

    You'll probably attract the attention of the FindGameObject… haters though :p, but you can deal with that after you get the basic functionality working.