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

[code] screen wrap [code]

Discussion in 'Scripting' started by maisaasaa, Nov 16, 2018.

  1. maisaasaa

    maisaasaa

    Joined:
    Nov 14, 2018
    Posts:
    35
    this is a code that is supposed to make the ship appear from left after it disappears in right but I don't get the errors showing in unity
    can someone tell me what their context means?



     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    You are declaring the FixedUpdate method inside the Update method. Don't do that.

    Also, post code directly into the forums using CODE tags instead of posting images. If you had, someone can just make a quick edit to your code to show you how to fix the issue, but not when you post images.
     
  3. maisaasaa

    maisaasaa

    Joined:
    Nov 14, 2018
    Posts:
    35

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ship : MonoBehaviour
    6. {
    7.  
    8.     // Use this for initialization
    9.     public Rigidbody2D RB;
    10.     public CircleCollider2D circle;
    11.     public int Thrustforce = 5;
    12.     int MoveUnitsPerSecond = 5;
    13.     public Vector2 thrustdirection = new Vector2(1, 0);
    14.     void Start()
    15.     {
    16.         RB = GetComponent<Rigidbody2D>();
    17.         circle = GetComponent<CircleCollider2D.Radius>();
    18.     }
    19.     void FixedUpdate()
    20.     {
    21.         RB.AddForce(Thrustforce * thrustdirection);
    22.     }
    23.  
    24.     void update()
    25.     {
    26.         float horizontalInput = Input.GetAxis("Thrust");
    27.         if (horizontalInput > 0)
    28.         {
    29.             Vector3 position = transform.position;
    30.             position.x += horizontalInput * MoveUnitsPerSecond * Time.deltaTime;
    31.             transform.position = position;
    32.         }
    33.     }
    34.         float buffer = 1.0f;
    35.         void FixedUpdate()
    36.         {
    37.             if (transform.position.x < ScreenUtils.screenLeft - buffer)
    38.             {
    39.                 transform.position = new Vector2(ScreenUtils.screenRight - 0.1f, transform.position.y);
    40.             }
    41.             if (transform.position.x > ScreenUtils.screenRight)
    42.             {
    43.                 transform.position = new Vector2(ScreenUtils.screenRight - 0.1f, transform.position.y);
    44.             }
    45.  
    46.        
    47.     }
    48. }
    I fixed the update mistake but now there's that single error and I think it's because I used fixedupdate twice when I renamed it, it got worse

     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Yeah now you have FixedUpdate defined twice. Once towards the top, and again towards the bottom. If you need the code in both, consolidate them to a single FixedUpdate method.
     
    sharkapps likes this.
  5. maisaasaa

    maisaasaa

    Joined:
    Nov 14, 2018
    Posts:
    35
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ship : MonoBehaviour
    6. {
    7.  
    8.     // Use this for initialization
    9.     public Rigidbody2D RB;
    10.     public CircleCollider2D circle;
    11.     public int Thrustforce = 5;
    12.     int MoveUnitsPerSecond = 5;
    13.     public Vector2 thrustdirection = new Vector2(1, 0);
    14.     void Start()
    15.     {
    16.         RB = GetComponent<Rigidbody2D>();
    17.         circle = GetComponent<CircleCollider2D.Radius>();
    18.     }
    19.  
    20.     void update()
    21.     {
    22.         float horizontalInput = Input.GetAxis("Thrust");
    23.         if (horizontalInput > 0)
    24.         {
    25.             Vector3 position = transform.position;
    26.             position.x += horizontalInput * MoveUnitsPerSecond * Time.deltaTime;
    27.             transform.position = position;
    28.         }
    29.     }
    30.     float buffer = 1.0f;
    31.     void FixedUpdate()
    32.     {
    33.         RB.AddForce(Thrustforce * thrustdirection);
    34.         if (transform.position.x < ScreenUtils.screenLeft - buffer)
    35.         {
    36.             transform.position = new Vector2(ScreenUtils.screenRight - 0.1f, transform.position.y);
    37.         }
    38.         if (transform.position.x > ScreenUtils.screenRight)
    39.         {
    40.             transform.position = new Vector2(ScreenUtils.screenRight - 0.1f, transform.position.y);
    41.         }
    42.     }
    43.  
    44.  
    45. }
    46.  
    47.  
    48.  
    when I join them together this happens
     
  6. PrieDayThor

    PrieDayThor

    Joined:
    Jun 18, 2015
    Posts:
    16