Search Unity

Why am I getting an error here?

Discussion in 'Scripting' started by BetrayedPickle, May 7, 2020.

  1. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
    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
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PickUp : MonoBehaviour {
    6.  
    7.     public bool isHolding;
    8.     public Transform theDest;
    9.  
    10.     void Input.GetKeyDown(KeyCode.E) && isHolding = false //from here down is where my errors are
    11.     {
    12.         GetComponent<Rigidbody>().useGravity = false;
    13.         this.transform.position.theDest.position;
    14.         this.transform.parent = GameObject.Find("Destination").transform;
    15.     }
    16.  
    17. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,695
    You are half-writing a function header (such as
    void Update()
    ) and (it looks like) half writing an if statement (
    if (blah)
    ).

    I do this a lot when I have had too much coffee. My approach is to take my hands away from the keyboard and breathe deeply several times, then resume.
     
  3. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
    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?
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Taking a wild guess what you meant to write:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PickUp : MonoBehaviour {
    6.  
    7.     public bool isHolding;
    8.     public Transform theDest;
    9.  
    10.     void Update()
    11.     {
    12.         if (Input.GetKeyDown(KeyCode.E) && isHolding == false) //from here down is where my errors are
    13.         {
    14.             GetComponent<Rigidbody>().useGravity = false;
    15.             this.transform.position.theDest.position;
    16.             this.transform.parent = GameObject.Find("Destination").transform;
    17.         }
    18.     }
    19.  
    20. }
    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.
     
  5. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
    Ok. I'll fix it. Why should I avoid GetComponent in Update and never put GameObject.Find in update?
     
  6. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
    Just put in your code, but I just noticed that I have some errors.
     

    Attached Files:

  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,695
    Uncle Joe didn't write that. It was from OP's original code line 13. I guess it should be:

    Code (csharp):
    1. this.transform.position = theDest.position;
    Every single character, including punctuation and capitalization, is critical to get right when programming. It's not like sending text messages.
     
    Joe-Censored likes this.
  8. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
    Yeah. I had the errors before I put the code in. But thank you, the errors are gone now.
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah I don't have VS on this computer and didn't really check any line of code that I wasn't actually editing :p 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.
     
    Ardenian likes this.
  10. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
    WHAT. There back. Here's a screenshot.
     

    Attached Files:

  11. BetrayedPickle

    BetrayedPickle

    Joined:
    Mar 30, 2019
    Posts:
    119
    I watched a c# tutorial series by blackthornprod, thats how I know anything at all
     
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,695
    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.
     
    Joe-Censored and Vryken like this.