Search Unity

GetComponent<Script>

Discussion in 'Scripting' started by ElectroCorp, Jun 18, 2021.

  1. ElectroCorp

    ElectroCorp

    Joined:
    Aug 25, 2020
    Posts:
    24
    I've been trying to get a system working to pick up and drop objects. My code looks like this:
    Code (CSharp):
    1. public class pickup : MonoBehaviour
    2. {
    3.     public bool ctrlKey;
    4.     public GameObject FirstPersonAIO;
    5.    
    6.    
    7.     void Update()
    8.     {
    9.         if (Input.GetMouseButtonDown(0))
    10.         {
    11.             RaycastHit hit;
    12.  
    13.             Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
    14.             if (Physics.Raycast(ray, out hit))
    15.             {
    16.                 Transform objectHit = hit.transform;
    17.                
    18.                
    19.                 Debug.Log("HIT!");
    20.                 if (objectHit.GetComponent<Script>pickupa.pick = true)
    21.                 {
    22.                     objectHit.transform.parent = FirstPersonAIO.transform;
    23.  
    24.                     objectHit.GetComponent<Rigidbody>().isKinematic = true;
    25.                 }
    26.                
    27.  
    28.  
    29.  
    30.  
    31.             }
    32.  
    33.         }
    34.  
    35.     }
    But it just says: GetComponent<Script> won't work. Any ideas?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,674
    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:




    Brackeys super-basic Unity Tutorial series:



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



    Basics of Unity GameObjects and Components:

     
    Bunny83 likes this.
  3. ElectroCorp

    ElectroCorp

    Joined:
    Aug 25, 2020
    Posts:
    24
    So I've modified the code and the GetComponent now refrences a script in the object it should hit. Here is the modified section:
    Code (CSharp):
    1. Debug.Log("HIT!");
    2.                 if (objectHit.GetComponent<pickupa>.pick = true)
    3.                 {
    4.                     objectHit.transform.parent = FirstPersonAIO.transform;
    5.  
    6.                     objectHit.GetComponent<Rigidbody>().isKinematic = true;
    7.                 }
    With pickupa being a script that tells this one weather or not the object can be picked up.
    However, it still produces an error:
    Code (CSharp):
    1. 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?)
    2.  
    3.  
     
  4. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    Code (CSharp):
    1. if (objectHit.GetComponent<pickupa>.pick = true)
    What's a pickupa?

    An Italian pickup maybe ;)
     
    Bunny83 and Kurt-Dekker like this.
  5. ElectroCorp

    ElectroCorp

    Joined:
    Aug 25, 2020
    Posts:
    24
    Haha! :D
    Sadly no, its the script that has a Boolean to tell this script weather or not it can be picked up.
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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 <brackets> 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.
     
    Bunny83 likes this.
  7. ElectroCorp

    ElectroCorp

    Joined:
    Aug 25, 2020
    Posts:
    24
    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.
     
  8. _met44

    _met44

    Joined:
    Jun 1, 2013
    Posts:
    633
    shouldn't this

    Code (CSharp):
    1. if (objectHit.GetComponent<pickupa>.pick)
    be this instead:

    Code (CSharp):
    1. if (objectHit.GetComponent<pickupa>().pick)
    or better:

    Code (CSharp):
    1. var myPickupa = objectHit.GetComponent<pickupa>();
    2. if (myPickupa != null && myPickupa.pick == true)
     
    Last edited: Jun 18, 2021
    Bunny83 and ElectroCorp like this.
  9. ElectroCorp

    ElectroCorp

    Joined:
    Aug 25, 2020
    Posts:
    24
    This seems to still produce the error.
    Tried:
    Code (csharp):
    1.  
    2. if (Physics.Raycast(ray, out hit))
    3.             {
    4.                 Transform objectHit = hit.transform;
    5.  
    6.                 var myPickupa = objectHit.GetComponent<pickupa>();
    7.                 if (myPickupa != null && myPickupa.pick = true)
    8.                 {
    9.                     objectHit.transform.parent = FirstPersonAIO.transform;
    10.  
    11.                     objectHit.GetComponent<Rigidbody>().isKinematic = true;
    12.                 }
    13.  
     
  10. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    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. :) Is your other script called pickupa?
     
  11. ElectroCorp

    ElectroCorp

    Joined:
    Aug 25, 2020
    Posts:
    24
    "Pickupa"(sorry that the name sounds stupid, I just go alphabetically) is a script in every Object in the scene.
    Code (csharp):
    1. The type or namespace name 'pickupa' could not be found
    Would I need to use a public variable?
     
  12. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    348
    Make sure to match that capitalization in your GetComponent.
     
    ElectroCorp likes this.
  13. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
     
    ElectroCorp likes this.
  14. ElectroCorp

    ElectroCorp

    Joined:
    Aug 25, 2020
    Posts:
    24
    Dang it! The capitalization was off. Also, was using =, not ==.
    IT WORKS! Thank you guys!
     
    mopthrow likes this.