Search Unity

I'm getting an error and I don't understand why. please read code?

Discussion in 'Getting Started' started by malachiddm, Jan 19, 2015.

  1. malachiddm

    malachiddm

    Joined:
    Dec 30, 2014
    Posts:
    44
    this is what it says, Assets/scripts/HatController.cs(29,34): error CS1729: The type `UnityEngine.Vector2' does not contain a constructor that takes `1' arguments

    and this is my code. I don't understand what is wrong in line 29 and 34 spaces over.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HatController : MonoBehaviour {
    5.  
    6.     public Camera cam;
    7.  
    8.     private float maxWidth;
    9.     private bool canControl;
    10.     public float xMin, xMax, yMin, yMax;
    11.     // Use this for initialization
    12.     void Start () {
    13.         if (cam == null) {
    14.             cam = Camera.main;
    15.         }
    16.         canControl = false;
    17.         Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
    18.         Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
    19.         maxWidth = targetWidth.x;
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.         if (canControl) {
    25.             transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
    26.             Vector3 rawPosition = Input.acceleration;
    27.         rigidbody2D.position = new Vector2(
    28.                 Mathf.Clamp (rigidbody2D.position.x, xMin, xMax)
    29.                 );
    30.        
    31.     }
    32. }
    33.     public void ToggleControl (bool toggle) {
    34.         canControl = toggle;
    35.     }
    36. }
     
  2. Deleted User

    Deleted User

    Guest

    The argument error is because in your Vector2 you only passed the x and not y, Vector2 is two floats x and y. So if you don't have to pass anything on y, this should fix it:

    rigidbody2D.position = new Vector2(Mathf.Clamp (rigidbody2D.position.x, xMin, xMax), 0.0f);
     
    angrypenguin likes this.
  3. malachiddm

    malachiddm

    Joined:
    Dec 30, 2014
    Posts:
    44
    Dude I love you. but now I have another problem. Now the accelerometer isnt allowing me to move my game object..
     
  4. Deleted User

    Deleted User

    Guest

    Don't know the output of your code as couldn't test it, but the problem I see is that you are clamping the rigidbody position on X in the rigidbody X axis, which for now just give me confusion, not sure if it will work but try this:

    float clampX = Mathf.Clamp (rigidbody2D.position.x, xMin, xMax);
    rigidbody2D.position = new Vector2(clampX, 0.0f);

    Delete the line I fixed before and replace with those two. Let me know how it goes, I can't test it for now, later I will see how to fix that.
     
  5. malachiddm

    malachiddm

    Joined:
    Dec 30, 2014
    Posts:
    44
    No for some reason my game object is still locked in place.
    this is the rest of my code.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5. public class GameController : MonoBehaviour {
    6.  
    7.     public Camera cam;
    8.     public GameObject ball;
    9.     public GameObject gameOverText;
    10.     public GameObject restartButton;
    11.     public GameObject splashScreen;
    12.     public GameObject startButton;
    13.     public HatController hatController;
    14.  
    15.  
    16.     private float maxWidth;
    17.     private bool playing;
    18.     public bool gameOver;
    19.    
    20.     // Use this for initialization
    21.     void Start () {
    22.         if (cam == null) {
    23.             cam = Camera.main;
    24.         }
    25.         playing = false;
    26.         Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
    27.         Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
    28.         float ballWidth = ball.renderer.bounds.extents.x;
    29.         maxWidth = targetWidth.x;
    30.  
    31.  
    32.     }
    33.     void FixedUpdate () {
    34.  
    35.  
    36. }
    37.     public void StartGame () {
    38.         gameOver = false;
    39.         splashScreen.SetActive (false);
    40.         startButton.SetActive (false);
    41.         hatController.ToggleControl (true);
    42.         StartCoroutine (Spawn ());
    43.         }
    44.  
    45.     IEnumerator Spawn () {
    46.                 yield return new WaitForSeconds (2.0f);
    47.         playing = true;
    48.                 while (!gameOver) {
    49.  
    50.                     Vector3 spawnPosition = new Vector3 (
    51.                         Random.Range (-maxWidth, maxWidth),
    52.                             transform.position.y,
    53.                             0.0f
    54.                     );
    55.                     Quaternion spawnRotation = Quaternion.identity;
    56.                     Instantiate (ball, spawnPosition, spawnRotation);
    57.                     yield return new WaitForSeconds (Random.Range (1.0f, 2.0f));
    58.                 }
    59.                 yield return new WaitForSeconds (2.0f);
    60.                 gameOverText.SetActive (true);
    61.                 yield return new WaitForSeconds (2.0f);
    62.                 restartButton.SetActive (true);
    63.         }
    64.  
    65.  
    66. }
    67.  
    68.  
     
  6. malachiddm

    malachiddm

    Joined:
    Dec 30, 2014
    Posts:
    44
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HatController : MonoBehaviour {
    5.  
    6.     public Camera cam;
    7.  
    8.     private float maxWidth;
    9.     private bool canControl;
    10.     public float xMin, xMax, yMin, yMax;
    11.     // Use this for initialization
    12.     void Start () {
    13.         if (cam == null) {
    14.             cam = Camera.main;
    15.         }
    16.         canControl = false;
    17.         Vector3 upperCorner = new Vector3 (Screen.width, Screen.height, 0.0f);
    18.         Vector3 targetWidth = cam.ScreenToWorldPoint (upperCorner);
    19.         maxWidth = targetWidth.x;
    20.     }
    21.    
    22.     // Update is called once per frame
    23.     void Update () {
    24.         if (canControl) {
    25.             transform.Translate(Input.acceleration.x, 0, -Input.acceleration.z);
    26.             Vector3 rawPosition = Input.acceleration;
    27.             float clampX = Mathf.Clamp (rigidbody2D.position.x, xMin, xMax);
    28.             rigidbody2D.position = new Vector2(clampX, 0.0f);
    29.  
    30.     }
    31. }
    32.     public void ToggleControl (bool toggle) {
    33.         canControl = toggle;
    34.     }
    35. }
     
  7. malachiddm

    malachiddm

    Joined:
    Dec 30, 2014
    Posts:
    44
    What I'm trying to do is have it so it clamps to the screen size that I'm playing on. Not have it so my game object goes out of the camera sight
     
  8. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Ok - a few things.

    First, is that your indenting is a bit confused, so it's not easy to read and check if everything is in the scope where it belongs.

    Make sure you have your code neat and clean. This will help a lot.

    This is a prime example:



    Is "ToggleControl" outside of the scope of the class? or not? I'll have to count each brace to make sure...

    Now, some specifics I can see in the code, without obviously seeing your issue:

    Here I see you getting the float "ballWidth" but never using it:


    Unimplemented FixedUpdate with bad indenting on the braces:


    This Update Function has a number of issues:


    The issues I see here are:
    • Bad indenting on the braces
    • Line 25: You are moving your Hat with transform.Translate before doing any of the other logic in this block of code. Shouldn't you try to move your Hat as the *last* step after doing all of the Input and Clamp logic?
    • Line 26: You are getting rawPosition from the InputAcceleration, but it's never used anywhere that I can see.
    • Line 27/28: You are clamping the rigidbody2D.position.x between xMin and xMax... What have you set these values to? These will be set in the inspector as they are plain public variables. If you don't set them to any value in the inspector, they will default to 0.0f - which means you will be clamping your Hat to (0,0). [Could this be the issue?]
    When reading code, remember code executes logically from the top down, and if the code is calling another function, e.g.: Transform.Translate(Vector3 value); the code will "pause" at that point as it calls the function and waits for the function to execute. Just make sure the steps you are writing are in the exact order you need them to execute. For some code, the order is irrelevant.

    This code:
    Code (csharp):
    1. void SomeFunction () {
    2.    float h = Input.GetAxis ("Horizontal");
    3.    float v = Input.GetAxis ("Vertical");
    4.    transform.Translate (h, 0, v);
    5. }
    ... will have the same results as this code:
    Code (csharp):
    1. void SomeFunction () {
    2.    float v = Input.GetAxis ("Vertical");
    3.    float h = Input.GetAxis ("Horizontal");
    4.    transform.Translate (h, 0, v);
    5. }
    It doesn't make a difference in which order we "get" our Input values, as long as both are gathered before we execute the Transform.Translate(Vector3 value); line.
    (NOTE: that the order of "float h" and "float v" are reversed in these two examples!)

    This code, on the other hand, will not work the same:
    Code (csharp):
    1. void SomeFunction () {
    2.    transform.Translate (h, 0, v);
    3.    float h = Input.GetAxis ("Horizontal");
    4.    float v = Input.GetAxis ("Vertical");
    5. }
    (As a matter of fact, that will generate an error, as we are using "h" and "v" before they are declared...)

    Try fixing these issues and look into your x/y Max/Min to see if they are 0, as we'll see how we get on.
     
  9. malachiddm

    malachiddm

    Joined:
    Dec 30, 2014
    Posts:
    44
    Thank you both so much. I'm blind, I kept putting xMin and xMax without setting it to the actual screen size. Thanks again to both of you. This is my first app and so the code from the tutorial videos along with my personal code made it clunky.
     
  10. malachiddm

    malachiddm

    Joined:
    Dec 30, 2014
    Posts:
    44

    So my game is finished and I went to build and run it on my phone and it works, But I have a major problem now that I don't understand. When I first click on the application to open it, it says powered by unity then I can play. So I click start and for some reason the spawn of the ball is centered to the screen and only spawns on that one location (UNTIL I GET A GAMEOVER) because then I press restart. It goes back to the splash screen, I press start and the ball spawns in all locations on the screen. This only happens on the build on my phone. If I close Unity and reopen it on the pc then I click play and it instantly starts spawning in all locations of the screen.
    What could cause this to happen? Is it just a bad build and should I delete the app then redownload it. It only happens for the first game when I fully close the application and restart it.