activate/deactivate objects

so basically, i cant find anything on how to implement a pick-up feature in my 3rd person game, then i thought… i would just have 2 objects, knife, and knife1 with their respective tags etc. (tags are knife and knife1 as well as the names to go with them), and the object “knife” is already in the player hand just deactivated, and when the player walks up to knife1 on the ground and presses a key knife1 would be deactivated and knife would be activated (showing the knife in player hand), i have like 0 c# experience so it was just an idea i had, heres the code i have currently

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class pickup : MonoBehaviour
{
    public GameObject knife;
    public GameObject knife1;
    void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "knife1" && Input.GetKeyDown(KeyCode.Space))
        {
            Debug.Log("Attempted to pick up object");
            knife.SetActive(true);
            knife1.SetActive(false);
        }
        else
            knife.SetActive(false);
            knife1.SetActive(true);
    }
   
}

im not even getting a debug message, no errors, nothing, im not sure as to why its not working… im also new to this so cut me a lil slackkkk

Hm, well, first is to put the Debug.Log() right at the start of the collision handler, and you can even use that to print the collision.gameObject.tag!

Second is to double-check you spelled (and capitalized) that method properly, and the type is correct, and that you are meeting ALL the requirements for the call to happen.

And finally, as a minor note, you should rename your variables to something like:

knifeInHand```

Otherwise next week you're gonna probably mix them up. :)

And here's my "don't spare the Debug.Log()!!!" blurb:

What is often happening in these cases is one of the following:

- the code you think is executing is not actually executing at all
- the code is executing far EARLIER or LATER than you think
- the code is executing far LESS OFTEN than you think
- the code is executing far MORE OFTEN than you think
- the code is executing on another GameObject than you think it is

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

- is this code even running? which parts are running? how often does it run? what order does it run in?
- what are the values of the variables involved? Are they initialized? Are the values reasonable?
- are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

https://discussions.unity.com/t/839300/3

OnCollisionEnter is called ONLY on the first frame that the colliders touch. GetKeyDown will also return true ONLY on the first frame that the button was pressed. So you would need to press the pick up key on the exact frame that the colliders touch for the pick up to work.

1 Like

i finally got it guys, thanks a lot for your help
heres the code if any of you wanted it for some odd reason, or if someone stumbles upon this and needs it

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enable : MonoBehaviour
{
    public GameObject knife;
    public GameObject knifehand;
    void OnTriggerEnter(Collider other)
    {
       
        if (other.gameObject.CompareTag("Player"))
        {
            Debug.Log("Collided with knife");
            knife.gameObject.SetActive(false);
            knifehand.gameObject.SetActive(true);
        }
    }
}