Ruby's 2D Adventure: Enemy Script not Working

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

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyController : MonoBehaviour
{
    public float speed = 3.0f;
    public bool vertical;
    public float changeTime = 3.0f;

    Rigidbody2D rigidbody2D;
    float timer;
    int direction = 1;

    // Start is called before the first frame update
    void Start()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
        timer = changeTime;
    }

    // Update is called once per frame
    void Update()
    {
        timer -= Time.deltaTime;

        if (timer < 0)
        {
            direction = -direction;
            timer = changeTime;
        }


        Vector2 position = rigidbody2D.position;

        if (vertical)
        {
            position.y = position.y + Time.deltaTime * speed * direction;
        }
        else
        {
            position.x = position.x + Time.deltaTime * speed * direction;
        }

        rigidbody2D.MovePosition(position);
    }
}

Thanks in advance

1 Like

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.

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

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”.

https://docs.unity3d.com/ScriptReference/MonoBehaviour.FixedUpdate.html

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
4633333--434491--upload_2019-6-11_0-48-10.png

Apologies if I misunderstood your problem.

My enemy isn’t moving at all and gives the error that it hides its inherited member

I thought in your first post you said that
there were no errors. What line does the error occur on?

Is this the error you are getting?
4636594--434974--upload_2019-6-11_20-1-59.png

When I click on it it takes me to this line.
4636594--434977--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.
4636594--434980--upload_2019-6-11_20-4-32.png

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

if enemy not moving, maybe you add script on gameobject directly not prefabs so script not updating. try delete enemy gameobject, then add again from prefabs folder

My Robot enemy is moving but Ruby main character not get damage when she touched the robot or collide with him.plz help.

This is my enemy script

public class EnemyController : MonoBehaviour
{
public float speed;
public bool vertical;
public float changeTime = 3.0f;

Rigidbody2D rigidbody2D;
float timer;
int direction = 1;

// Start is called before the first frame update
void Start()
{
rigidbody2D = GetComponent();
timer = changeTime;
}

// Update is called once per frame
void Update()
{

timer -= Time.deltaTime;

if (timer < 0)
{
direction = -direction;
timer = changeTime;
}

Vector2 position = rigidbody2D.position;
if (vertical)
{
position.y = position.y + Time.deltaTime * speed;
}
else
{
position.x = position.x + Time.deltaTime * speed;
}

rigidbody2D.MovePosition(position);
}

void OnCollisionEnter2D(Collision2D other)
{
RubyController player = other.gameObject.GetComponent();

if (player != null)
{
player.ChangeHealth(-1);
}
}

}

same issue here. robot enemy is not moving. the script looks the same as above.

solved it, hadn’t set my speed so it was at 0.

FYI, there’s a thread dedicated to Ruby’s Adventure 2D over here where you might find answers to your questions sooner…

Yeah, despite the script saying “3.0f” the Inspector window still shows “0” so you have to set a number there. I’m using 2019.2.0.

One problem I noticed is that the robot guy will move when Ruby touches him. She can basically move him around the map. Granted, I’m still only on that particular chapter. Haven’t gone further yet.

maybe a bit late but you need to make sure that in the inspector window in the enemy controller script the value of speed isnt set on 0, but 1 or 2

1 Like

mine doesnt turn arround, starts going right, or up if i check the “vertical” in the inspector, but only goes in one direction. anyone know how to fix this?

Similarly, check that the Change Time variable in the Enemy Controller Script in the Inspector Window is not set to 0.

So I had a couple issues and here is how I fixed them:

  1. My bot didn’t have Rigid Body 2D with gravity set to 0. Somehow this got lost when I was playing around with my prefabs. It didn’t throw an error in the code, so it wasn’t until I checked what modules I had on my prefab I realized anything was wrong.

  2. In the example code, they have public class EnemyController2 : MonoBehaviour. This needs to match the script file name, so I updated this to public class EnemyController : MonoBehaviour

  3. Set the speed of the bot either in the code to start at not zero, or update it when you start the game in the inspector window