GetComponent<Script>

I’ve been trying to get a system working to pick up and drop objects. My code looks like this:

public class pickup : MonoBehaviour
{
    public bool ctrlKey;
    public GameObject FirstPersonAIO;
    
   
    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;

            Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
            {
                Transform objectHit = hit.transform;
               
               
                Debug.Log("HIT!");
                if (objectHit.GetComponent<Script>pickupa.pick = true)
                {
                    objectHit.transform.parent = FirstPersonAIO.transform;

                    objectHit.GetComponent<Rigidbody>().isKinematic = true;
                }
               




            }

        }

    }

But it just says: GetComponent won’t work. Any ideas?

Do you have a Component called Script ?

If you do not and you don’t understand why I ask that question, then set this code aside and do some real-world Unity tutorials, such as these:

Imphenzia / imphenzia - super-basic Unity tutorial:

https://www.youtube.com/watch?v=b3DOnigmLBY

https://www.youtube.com/watch?v=pwZpJzpE2lQ

Brackeys super-basic Unity Tutorial series:

https://www.youtube.com/watch?v=IlKaB1etrik

Sebastian Lague Intro to Game Development with Unity and C#:

https://www.youtube.com/watch?v=_cCGBMmMOFw

Basics of Unity GameObjects and Components:

https://www.youtube.com/watch?v=9Nf2_ds5y8c

1 Like

So I’ve modified the code and the GetComponent now refrences a script in the object it should hit. Here is the modified section:

Debug.Log("HIT!");
                if (objectHit.GetComponent<pickupa>.pick = true)
                {
                    objectHit.transform.parent = FirstPersonAIO.transform;

                    objectHit.GetComponent<Rigidbody>().isKinematic = true;
                }

With pickupa being a script that tells this one weather or not the object can be picked up.
However, it still produces an error:

Assets\pickup.cs(24,44): error CS0246: The type or namespace name 'pickupa' could not be found (are you missing a using directive or an assembly reference?)
if (objectHit.GetComponent<pickupa>.pick = true)

What’s a pickupa?

An Italian pickup maybe :wink:

2 Likes

Haha! :smile:
Sadly no, its the script that has a Boolean to tell this script weather or not it can be picked up.

There are at least four things wrong with that if statement, to the point that it’s hard to tell what you’re actually trying to do with it, and therefore hard to say what it would look like if correct. Could you describe that?

What you put in the must always be a class name, that is, the same thing (exact spelling and capitalization) as you have in some “public class THING : MonoBehaviour” somewhere. This error means you don’t have a class named “pickupa” anywhere.

1 Like

Here is what should happen.

  1. mouse click here A ray is sent from the camera.
  2. When the ray hits something, it gathers data about it.
  3. The if statement checks a script that is in every object in the scene, to see if a Boolean in that script(public bool pick) is True or False(1, 0),

Just also realized that in C and other languages like it Booleans are 1 and 0… doesn’t change the error though.

shouldn’t this

if (objectHit.GetComponent<pickupa>.pick)

be this instead:

if (objectHit.GetComponent<pickupa>().pick)

or better:

var myPickupa = objectHit.GetComponent<pickupa>();
if (myPickupa != null && myPickupa.pick == true)
2 Likes

This seems to still produce the error.
Tried:

if (Physics.Raycast(ray, out hit))
            {
                Transform objectHit = hit.transform;

                var myPickupa = objectHit.GetComponent<pickupa>();
                if (myPickupa != null && myPickupa.pick = true)
                {
                    objectHit.transform.parent = FirstPersonAIO.transform;

                    objectHit.GetComponent<Rigidbody>().isKinematic = true;
                }

Sorry, my fault for trying to joke in the first place, I shouldn’t have assumed that was clear at all.

I meant your error refers to pickup.cs, but your GetComponent is trying to get a script called pickupa.cs. :slight_smile: Is your other script called pickupa?

“Pickupa”(sorry that the name sounds stupid, I just go alphabetically) is a script in every Object in the scene.

The type or namespace name 'pickupa' could not be found

Would I need to use a public variable?

Make sure to match that capitalization in your GetComponent.

1 Like

Is it capitalized as you wrote it here? Capitalization/spelling must match exactly.

Also: For checking the .pick value, you’ll want to use ==, not =. Two == is checking for equality; one = is assignment, which C# correctly doesn’t let you do in an if statement.

1 Like

Dang it! The capitalization was off. Also, was using =, not ==.
IT WORKS! Thank you guys!

1 Like