Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

how to fix code

Discussion in 'Scripting' started by rileydj2009, Apr 23, 2021.

  1. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    i got two error messages but i have no idea what the mean im still a begginer could anybody help me
     

    Attached Files:

  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You're trying to run the UpdateMouseLook() function outside of any function. You're also apparently declaring a new Update() function inside of a function, but providing no contents to it. I suggest getting a C# book or following a C# tutorial, as these are C# syntax issues.
     
    Kurt-Dekker likes this.
  3. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    i litterly did exactly as the tutorial did exact how is it working for them but not me
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I guarantee that the code in the tutorial is not identical to yours. Yours is not valid C# and will not compile, for the reasons I already stated.
     
    Brathnann likes this.
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,144
    No you didn't. Because they didn't get this error.
     
    Joe-Censored likes this.
  6. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    screw it i give up im not good near good enough for this code
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The problem is you don't know C# language syntax. Learn that first. Game development requires a considerable amount of learning and patience.
     
  8. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    do you think i should keep going?
     
  9. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yes, but I'd stop working on Unity for now, and just focus on learning C# first. Once you have the basics down, how to declare methods, where C# has you write your code, then come back to the Unity. It will make a lot more sense at that point.
     
    Yoreki likes this.
  10. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,590
    That solely depends on you. Do you want to develop games? If you really want to, you can. However, if you are not willing to go through the effort of learning how to do it, which can involve a lot of patience at first, then it may not be for you. Only you know the answer.
    As @Joe-Censored recommended, you may wanna start with learning C# outside of Unity first. But there are also good tutorials covering both at the same time. I can recommend the series on gamedevelopment by Sebastian Lague on youtube, which starts at 0 knowledge, but obviously cannot cover every aspect of both Unity and C#. It may be a good entry point from where you can continue yourself tho.
     
    Joe-Censored likes this.
  11. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    yes ok is there any videos you can link to me?
     
  12. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    its my dream to develop games its just so stressful at first
     
  13. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    I personally can't, as I taught myself C# using books. Video tutorials weren't really a thing at the time. Hopefully someone else can make suggestions.
     
  14. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    ah ok
     
  15. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    hm i dont have any errors but again my code wont work
     
  16. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
  17. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    thank you and yeah i dont copy and paste i normaly follow the video to understand it more even if it means spending hours longer on it
     
  18. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    i keep getting this error message do you have any idea how to fix it ?
     

    Attached Files:

  19. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    That isn't an error message, that is a warning. Errors are in red.

    What it is saying though is in playercontroller.cs, on line 7, you are defining a variable called "playerCamera". But nowhere in the project do you ever use it. It is just trying to be helpful in case you forgot to write the code which uses it. I'd assume that code would be introduced later in the tutorial, so just ignore it.

    It is a pretty common warning you'll see when writing scripts you're not yet finished with yet, as you'll often add variables which you have plans for, but have not yet written the code which uses them. I only pay attention to that warning if it is for code which I thought I was done with. On rare occasion I go "woops!" when I investigate, but usually it is because I redesigned the script later to no longer use that variable, but simply forgot to remove it entirely.
     
  20. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    oh
     
  21. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    it still wont work im gonna finish this movement then start studying c++ more but could you guide me along while im trying to finish the movement?
     
  22. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    C++ is good to learn. Also look at c# since that's the language Unity uses. But I do recommend looking at C and cpp also
     
  23. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    yeah i still have no idea why this script isint working right
     

    Attached Files:

    • code.png
      code.png
      File size:
      34.3 KB
      Views:
      254
  24. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    This is message that you have code what is not used just put // infront of code or delete it.
     
  25. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    You really try to learn some c# basics first in your code you have method but you never calling it just put

    UpdateMouseLook(); inside Update method.

    Code (CSharp):
    1. void Update()
    2.  
    3. {
    4. UpdateMouseLook();
    5. }
     
    Joe-Censored likes this.
  26. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    also good thing is to use debug log so then you can see where your code stop, make edit what i wrote and then put this to your UpdateMouseLookMethod() so then you will be sure that program run into your method and then if it will still not work you will be sure that problem is inside UpdateLookMethod.

    Code (CSharp):
    1. UpdateMouseLook()
    2.  
    3. {
    4. Debug.Log("some text what will be displayed in console");
    5. }
     
  27. rileydj2009

    rileydj2009

    Joined:
    Apr 20, 2021
    Posts:
    38
    ok how do i fix the one i just posted in the tutorial im watching they played it and where able to look up and down so how do i fix that so it work
    do you mean like this?
     

    Attached Files:

  28. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    1)
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Assets.Scripts
    4. {
    5.     public class NewMonoBehaviour1 : MonoBehaviour
    6.     {
    7.         float mouseSensivity = 3.5f;
    8.  
    9.          void Update()
    10.         {
    11.             UpdateMouseLook();
    12.         }
    13.         void UpdateMouseLook()
    14.         {          
    15.             Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
    16.             transform.Rotate(Vector3.up * mouseDelta.x * mouseSensivity);
    17.         }
    18.     }
    19. }
    2)
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. namespace Assets.Scripts
    4. {
    5.     public class NewMonoBehaviour1 : MonoBehaviour
    6.     {
    7.         float mouseSensivity = 3.5f;
    8.  
    9.          void Update()
    10.         {              
    11.             Vector2 mouseDelta = new Vector2(Input.GetAxis("Mouse X"), Input.GetAxis("Mouse Y"));
    12.             transform.Rotate(Vector3.up * mouseDelta.x * mouseSensivity);
    13.         }
    14.     }
    15. }

    Both do same but in first it is nicer and more clean.
     
  29. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    In the last code you posted, the X axis of the mouse is saved as mouseDelta.x (left and right mouse movement), while the Y axis of the mouse is saved as mouseDelta.y (up and down). But you never use mouseDelta.y anywhere. So you're missing the code for looking up and down still.

    It might look something like this, but I haven't looked at the tutorial.

    Code (csharp):
    1. transform.Rotate(Vector3.right * mouseDelta.y * mouseSensivity);
    It might also be handled in a different script, since it is pretty common to separate your up/down vs your left/right movement onto different objects.