Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Ruby's 2D Adventure: Enemy Script not Working

Discussion in '2D' started by girlinorbit, May 9, 2019.

  1. girlinorbit

    girlinorbit

    Joined:
    Apr 27, 2019
    Posts:
    2
    I am using the 'Ruby's 2D Adventure' tutorial to get a basic introduction into Unity, and I've hit a pretty big stumbling block. I am as far as 'World Interactions - Damage Zones & Enemies' part 4, and am stuck on the script. The script 'solution' given does not work for me. It is attached to the prefab, and I get not errors.
    .
    The enemy is a robot who is supposed to move up and down. Here is the script. I am using Unity version 2018.3, which is the suggested version for this course.

    EnemyControler.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyController : MonoBehaviour
    6. {
    7.     public float speed = 3.0f;
    8.     public bool vertical;
    9.     public float changeTime = 3.0f;
    10.  
    11.     Rigidbody2D rigidbody2D;
    12.     float timer;
    13.     int direction = 1;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         rigidbody2D = GetComponent<Rigidbody2D>();
    19.         timer = changeTime;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         timer -= Time.deltaTime;
    26.  
    27.         if (timer < 0)
    28.         {
    29.             direction = -direction;
    30.             timer = changeTime;
    31.         }
    32.  
    33.  
    34.         Vector2 position = rigidbody2D.position;
    35.  
    36.         if (vertical)
    37.         {
    38.             position.y = position.y + Time.deltaTime * speed * direction;
    39.         }
    40.         else
    41.         {
    42.             position.x = position.x + Time.deltaTime * speed * direction;
    43.         }
    44.  
    45.         rigidbody2D.MovePosition(position);
    46.     }
    47. }
    Thanks in advance
     
    BryanLyon likes this.
  2. beanie4now

    beanie4now

    Joined:
    Apr 22, 2018
    Posts:
    308
    Looks fine to me as a standalone script. I would guess the instance in your game isn't the same as your prefab. Or you have the script disabled. Or one of a million things in unity that can go wrong. The script itself looks fine tho.
     
  3. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,524
    Your trying to move a rigidbody in update, you should be using FixedUpdate for any physics

    Also make sure it actually finds the rigidbody component and you are using the right component, Rigidbody2D and not Rigidbody on your object
     
  4. BryanLyon

    BryanLyon

    Joined:
    Jun 3, 2019
    Posts:
    1
    What does that mean "Your trying to move a rigidbody in update", and "You should be using FixedUpdate for any physics"?

    I am doing this tutorial and am having this same problem. I fix or alternative would be very helpful or an explanation about "Update", and "FixedUpate".
     
  5. GameDevCouple_I

    GameDevCouple_I

    Joined:
    Oct 5, 2013
    Posts:
    1,524
    https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html
     
  6. Kenbev84

    Kenbev84

    Joined:
    Mar 3, 2019
    Posts:
    2
    Is it that your enemy is just moving horizontally? The script won't automatically change the enemy from going side-to-side to going up-and-down. As bool vertical is public it creates a check box in the Unity editor. Ticking that should make the enemy move vertically
    upload_2019-6-11_0-48-10.png

    Apologies if I misunderstood your problem.
     
  7. dwest13542

    dwest13542

    Joined:
    Jun 2, 2019
    Posts:
    1
    My enemy isn't moving at all and gives the error that it hides its inherited member
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    1,163
    I thought in your first post you said that
    there were no errors. What line does the error occur on?
     
  9. Kenbev84

    Kenbev84

    Joined:
    Mar 3, 2019
    Posts:
    2
    Is this the error you are getting?
    upload_2019-6-11_20-1-59.png

    When I click on it it takes me to this line.
    upload_2019-6-11_20-3-31.png

    If you put "new" in front of it it will get rid of the error... Well it did for me anyway.
    upload_2019-6-11_20-4-32.png

    As to why the enemy is not moving, can't really tell without seeing your script.