I know this is super obvious and I’m kind of embarrassed to ask this but…
Why am I getting an error here? Here is my code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUp : MonoBehaviour {
public bool isHolding;
public Transform theDest;
void Input.GetKeyDown(KeyCode.E) && isHolding = false //from here down is where my errors are
{
GetComponent<Rigidbody>().useGravity = false;
this.transform.position.theDest.position;
this.transform.parent = GameObject.Find("Destination").transform;
}
}
lol, I haven’t had too much coffee I just don’t know what the heck I’m doing. I’m following a tutorial for a pickup script but he is making it so when you click, you pick it up. but I want to make it so when I press e, I pick it up. please tell me how i do that?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickUp : MonoBehaviour {
public bool isHolding;
public Transform theDest;
void Update()
{
if (Input.GetKeyDown(KeyCode.E) && isHolding == false) //from here down is where my errors are
{
GetComponent<Rigidbody>().useGravity = false;
this.transform.position.theDest.position;
this.transform.parent = GameObject.Find("Destination").transform;
}
}
}
So I just added the Update declaration, turned what is probably an “if” statement actually into one, and turned your assignment of isHolding into a comparison.
Though this code is a terrible idea. You should try to avoid GetComponent in Update when possible, and you should never put GameObject.Find in Update. For performance reasons. Actually you should never use GameObject.Find at all, as there is always a better option. But there are endless discussions on that topic if you’re interested.
Yeah I don’t have VS on this computer and didn’t really check any line of code that I wasn’t actually editing I mostly just demonstrated what Kurt already suggested.
Good luck with your Unity journey
You might want to consider a general C# tutorial, so you understand classes, methods, etc, so can spot these issues yourself immediately. These weren’t actually Unity issues, but basic C# syntax issues. A Unity tutorial would generally not cover basic C# syntax, since it is an industry standard language created by Microsoft not Unity.
I think you better work back through some C# basics. You’re conflating classes and function headers and block conditional controls at a minimum, not to mention getting all kinds of syntax twisted up.