Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question I get this error: error CS1022: Type or namespace definition, or end-of-file expected

Discussion in 'Getting Started' started by graf_tueringen, Oct 23, 2023.

  1. graf_tueringen

    graf_tueringen

    Joined:
    Oct 23, 2023
    Posts:
    2
    Hello there
    I am trying to fix this error(error CS1022: Type or namespace definition, or end-of-file expected) for a long time but i don't know what is wrong. It would be nice if someone can help me.


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class CameraFollow : MonoBehaviour
    7. {
    8. // Start is called before the first frame update
    9. void Start()
    10. {
    11. private float y = transform.position.y;
    12. private float x = transform.position.x;
    13. }
    14. public GameObject car;
    15. private float xPosition;
    16. private float yPosition;
    17. private float alpha;
    18. // Update is called once per frame
    19. void LateUpdate()
    20. {
    21. alpha = car.transform.rotation.y;
    22. xPosition = Mathf.Cos(alpha) * 7;
    23. yPosition = Mathf.Sin(alpha)* 7;
    24. x = car.transform.position.x + xPosition;
    25. y = car.transform.position.y + yPosition;
    26. }
    27. }
    28.  
    29.  
    30.  
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,423
    If you looked at the line numbers on the error (you didnt supply here) it almost certainly points to the code on line 11 here, which says private float y .. you cant declare private/public inside methods.
     
    Bunny83 likes this.
  3. graf_tueringen

    graf_tueringen

    Joined:
    Oct 23, 2023
    Posts:
    2
    the code looks like this now but now i get an other error: Assets/Scrpts/CameraFollow.cs(27,30): error CS0019: Operator '+' cannot be applied to operands of type 'Vector3' and 'float'

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class CameraFollow : MonoBehaviour
    6. {
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.  
    11.     }
    12.         public GameObject car;
    13.         private float xPosition;
    14.         private float yPosition;
    15.         private float alpha;
    16.        
    17.     // Update is called once per frame
    18.     void LateUpdate()
    19.     {
    20.         alpha = car.transform.rotation.y;
    21.         xPosition = Mathf.Cos(alpha) * 7;
    22.         yPosition = Mathf.Sin(alpha)* 7;
    23.         transform.position = car.transform.position + xPosition;
    24.         transform.position = car.transform.position + yPosition;
    25.        
    26.    
    27.     }
    28. }
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,847
    Those two lines:
    don't make sense.
    transform.position
    is a Vector3 property. So whatever you assign to this property has to be a Vector3.
    car.transform.position
    is also a Vector3. However
    xPosition
    is just a float variable. As the error tells you, it makes no sense to add a Vector3 and a float together. A Vector3 is 3 floats combined. You probably wanted to do

    Code (CSharp):
    1. transform.position = car.transform.position + new Vector3(xPosition, yPosition, 0);
    However apart from that syntactical issue, this line also doesn't make sense:

    Code (CSharp):
    1. alpha = car.transform.rotation.y;
    The rotation property is a Quaternion and it's y component is not an angle. If you want the camera being offset behind the car on the x-z- plane, you probably want to do something like:

    Code (CSharp):
    1.     void LateUpdate()
    2.     {
    3.         Vector3 dir = car.transform.forward;
    4.         dir.y = 0f;
    5.         dir = dir.normalized * 7;
    6.         transform.position = car.transform.position - dir;
    7.     }
     
    bugfinders likes this.