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?
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?)
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.
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.