Help im new to this

I’m making a 2d top down zombie shooter with some friends through Unity collaborate but we ran in to this issue with the zombie code that we wrote.

here is the code:

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

public class Zombie : MonoBehaviour
{

public float speed = 3f;
public float detectDistance = 10f;
[SerializedField] Transform target;

private void Update()
{
if (target != null) {
float offset = -90f;
Vector2 _dir = target.position - transform.position;
_dir.Normalize();
float _angle = Mathf.Atan2(_dir.y, _dir.x) * Mathf.Rad2Deg;

transform.rotation = Quaterion.Euler(new Vector3(of, 0f, _anfle + offset));

transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.DeltaTime);

}
}

private void LateUpdate()
{
target = FindTarget();
}

Transform FindTarget()
{
if (target == null) {
Transform _target = GameObject.FindGameObjectWithTag(“Player”).transform;
if (_target != null)
{
float _dist = Vector2.Distance(_target.position, transform.position);

if (_dist <= detectDistance)
{
return _target;
}
else
{
return null;
}
}
else if
{
return null;
}
else
{
float _dist = Vector2.Distance(target.position, transform.position);

if (_dist <= detectDistance)
{
return target;
}
else
{
return null;
}
}
}
return target;
}
}

the code got a bit messy here is a pic if it helps:

Here’s how to properly share your code on the forums: Using code tags properly

By reading your error message you can easily find the problem. Your error is on line 51 as the error messages say. Note the (51, 20) in the error messages. That means “Line 51, column 20”.

The problem is you have an if there with no condition! An if statement needs to have a condition! Notice all of your other if statements have conditions like if (_dist < detectDistance) but that one is just… empty!

1 Like

ty, but i dont know what to have in this condition. dont even know if i need this if

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

public class Zombie : MonoBehaviour
{

    public float speed = 3f;
    public float detectDistance = 10f;
    [SerializedField] Transform target;


 
    private void Update()
    {
        if (target != null) {
            float offset = -90f;
            Vector2 _dir = target.position - transform.position;
            _dir.Normalize();
            float _angle = Mathf.Atan2(_dir.y, _dir.x) * Mathf.Rad2Deg;

            transform.rotation = Quaterion.Euler(new Vector3(of, 0f, _anfle + offset));

            transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.DeltaTime);

        }
    }

    private void LateUpdate()
    {
        target = FindTarget();
    }

    Transform FindTarget()
    {
        if (target == null) {
            Transform _target = GameObject.FindGameObjectWithTag("Player").transform;
            if (_target != null)
            {
                float _dist = Vector2.Distance(_target.position, transform.position);

                if (_dist <= detectDistance)
                {
                    return _target;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
            else
            {
                float _dist = Vector2.Distance(target.position, transform.position);

                if (_dist <= detectDistance)
                {
                    return target;
                }
                else
                {
                    return null;
                }
            }
        }
        return target;
    }
}

@Leander_Ns Did you write this code? Or did you copy it from somewhere. If you do not understand the code or do not know why you need it, you probably should not be using it. Also, you cannot have two else statements as you have. It’s like the following analogy “If there is no gas in my car, I will stay home. Or else I will drive to the beach. Or else I will drive to the mountains.” So where does this person go if there is gas in the car? That is what the compiler is complaining about. I hope this helps.

1 Like

It sounds like you’re not doing tutorials properly. Specifically, it sounds like you haven’t done Step #1 (because of all the typo errors you made that you must now fix) and you also need to do Step #2… AFTER you do Step #1. See below.

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Tutorials are a GREAT idea. Tutorials should be used this way:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.
Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

Finally, when you have errors…

Remember: NOBODY here memorizes error codes. That’s not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

This assumes you have at least written and studied some code and have run into some kind of issue.

If you haven’t even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.

If you just want someone to do it for you, you need go to one of these places:

Ty for the obvious way to follow a tutorial, and as you say these forums are here for people that are ready to code. and i can guaranty I am, but we all got to start somewhere right? yet though I’m a starter I get called out for a bunch of unnecessary bs text where yall (BIG PART OF THE COMMUNITY SOO FAR) generally experienced people try to pin point that I’m stopid without saying it (this is not my first encounter and I’m not calling YOU out for being this way) but just my general experience so far. cant say its pleasing and it certainly doesn’t motivate me to go on as this community soo far is worse than a cod lobby only to find that the toxic people are 40+ yr old data nerds. no offence and none should be taken as I’ve got worse from the community.

ok now as i got that out the way i wanna actually respond to your reply. yes i did get this code from a tutorial. yes i did follow it and even watched through it 4-5 times just to see if i did any mistakes’ yes I’m new to programming so everything wont be perfect. Yet I am STUCK soo please I ask any of you that read this that know how to solve my problem don’t give me some damn riddle of word that i haven’t learnt yet and just be strait forward on how to fix it. i would be really grateful.

PS here is the tutorial vid i followed:

https://www.youtube.com/watch?v=6Lxc27gg9DA

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

You may edit your post above.

These ARE your mistakes and as far as I can tell, it is just your own typographic errors.

7985586--1025769--Screen Shot 2022-03-22 at 2.03.49 PM.png

You don’t have to find them. The compiler found them for you. You do have to go fix them however.

ALSO, nobody is going to go watch a perfectly good tutorial to pretend we make the same typos as you did. How on earth could that be useful? Go fix your typos.

Since everybody makes typos, here is how to fix your own typos:

The complete error message contains everything you need to know to fix the error yourself.

The important parts of the error message are:

  • the description of the error itself (google this; you are NEVER the first one!)
  • the file it occurred in (critical!)
  • the line number and character position (the two numbers in parentheses)
  • also possibly useful is the stack trace (all the lines of text in the lower console window)

Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don’t have to stop your progress and fiddle around with the forum.

Just a quick skim of the code and I can see half a dozen or more typos:

  • _anfle instead of _angle
  • Quaterion instead of Quaternion
  • SerializedField instead of SerializeField
  • a blank if statement followed by a block of code
  • typing of instead of 0f

That’s just what popped out to me in 20 seconds of scanning. Now go fix them like every good programmer does.

thank you for finally being direct and not bull shiting all around

how do i fix these tho?
7985649--1025790--upload_2022-3-22_22-28-2.png

You have two else statements. You’ll need to remove one of them. It could be what is shown in red, but it’s just a guess

7985670--1025796--Else.png

Closed for being lazy and argumentative. Answers have been provided to what are all sloppy typing not being being careful. The debugger tells you what they errors are and where they are. These forums to help with specific problems and learning. Not to proofread sloppy typing. Additionally don’t be rude with folks trying to provide assistance.
Additionally read the forum rules and post properly next time.