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
  4. Dismiss Notice

help with script

Discussion in 'Scripting' started by Undlien, Mar 21, 2022.

Thread Status:
Not open for further replies.
  1. Undlien

    Undlien

    Joined:
    Mar 17, 2022
    Posts:
    5
    i dont know whats wrong with this script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Zombie : MonoBehaviour
    6. {
    7.  
    8.     public float speed = 3f;
    9.     public float detectDistance = 10f;
    10.     [SerializedField] Transform target;
    11.  
    12.  
    13.  
    14.     private void Update()
    15.     {
    16.         if (target != null) {
    17.             float offset = -90f;
    18.             Vector2 _dir = target.position - transform.position;
    19.             _dir.Normalize();
    20.             flaot _angle = MathF.Atan2(_dir.y, _dir.x) * MathF.Rad2Deg;
    21.  
    22.             transform.rotation = Quaterion.Euler(new Vector3(of, 0f, _anfle + offset));
    23.  
    24.             transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.DeltaTime);
    25.  
    26.         }
    27.     }
    28.  
    29.     private void LateUpdate()
    30.     {
    31.         target = FindTarget();
    32.     }
    33.  
    34.     Transform FindTarget()
    35.     {
    36.         if (target == null) {
    37.             Transform _target = GameObject.FindGameObjectWithTag("Player").transform;
    38.             if (_target != null)
    39.             {
    40.                 float _dist = Vector2.Distance(_target.position, transform.position);
    41.  
    42.                 if (_dist <= detectDistance)
    43.                 {
    44.                     return _target;
    45.                 }
    46.                 else
    47.                 {
    48.                     return null;
    49.                 }
    50.             }
    51.  
    52.             else
    53.             {
    54.                 return null;
    55.             }
    56.             else
    57.             {
    58.                 float _dist = Vector2.Distance(target.position, transform.position);
    59.  
    60.                 if (_dist <= detectDistance)
    61.                 {
    62.                     return target;
    63.                 }
    64.                 else
    65.                 {
    66.                     return null;
    67.                 }
    68.             }
    69.         }
    70.         return target;
    71.     }
    72. }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    How would we know what's wrong with it? We don't even know what it being correct would look like because you didn't explain it so you need to post some information. Sure, we can guess but we shouldn't have to. ;)

    I'll move your post to the Scripting forum though where general scripting questions should go.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    I'll add that the code above won't even compile because it has a whole bunch of typos in it.

    "flaot" should be "float"
    "MathF" should be Mathf
     
  4. Undlien

    Undlien

    Joined:
    Mar 17, 2022
    Posts:
    5
    I get an error on line 55 (a bracket). I am trying to make enemy ai
     
  5. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    Can you use else twice in one if statement?

    Code (CSharp):
    1.             else
    2.             {
    3.                 return null;
    4.             }
    5.             else
    You should return something in all cases in if statements also :)
     
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    I'm sure you do, you've got lots of typos and just plain wrong C#. You didn't acknowledge the errors I mentioned, just came and posted about some other error. Did you fix them? Help is about communicating both ways, not just posting new errors.
     
  7. Undlien

    Undlien

    Joined:
    Mar 17, 2022
    Posts:
    5
    didn't mean to make you angry, I'm new to c#, I just want my game to work, and I've got some errors, I don't know what else to ask
     
  8. Undlien

    Undlien

    Joined:
    Mar 17, 2022
    Posts:
    5
    I fixed some errors, do you see any mistakes in my SerializedField. I'm kinda stuck...
     
  9. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    I'm not angry. I'm saying you should say if you've fixed what's been mentioned at least, not just state more errors.

    Please note that all you've said is "i dont know whats wrong with this script" and have NOT stated what you're seeing being reported.
     
  10. Undlien

    Undlien

    Joined:
    Mar 17, 2022
    Posts:
    5
    I believe I fixed the error on line 55 with else if instead of else.
     
  11. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    424
    MelvMay, I was the same when I started C#, I didn't know my arse from my elbow and didn't know what to ask, I'm glad I didn't get the same welcome he got.
    I understood him clearly and didn't feel the need to go through the usual programmer's protocol to get an answer to his question.
     
    Undlien likes this.
  12. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    Asking for more information and asking to acknowlede what has been fixed is a simple communication thing. I'm glad you knew exactly what all the problems were, not all will. This is why we ask and highlight to devs, especially when they begin here, to spend a little more time clearly communicating the errors such as actually stating the errors being reported and what they've fixed based upon feedback.

    We deal with many of these posts each and every day.
     
  13. Chubzdoomer

    Chubzdoomer

    Joined:
    Sep 27, 2014
    Posts:
    106
    I have a feeling this is another classic case of Visual Studio not being properly "paired" with Unity, because Intellisense would help avoid most of these errors (like float being misspelled "flaot"). There should also be red squiggly lines galore, making it trivial to track down where most of the typos/errors are.

    To the OP:
    In Unity, make sure you go to Edit->Preferences->External Tools and change "External Script Editor" to your version of Visual Studio (that is, of course, assuming you aren't using some other IDE).

    If you aren't seeing red squiggly lines all throughout your script (particularly where the typos are), you absolutely should be!
     
    Last edited: Mar 21, 2022
    SamTheLearned likes this.
  14. theonerm2_unity

    theonerm2_unity

    Joined:
    Sep 7, 2018
    Posts:
    130
    I feel you guys on these forums are too cold. I mean you expect everyone to know exactly what they are talking about and know how to explain it. I mean sure... They should at least say what they expect the script to do but you can't expect everyone who uses Unity to be an expert. I consider myself to be pretty decent with Unity but not an expert. There are some things I can't even explain to you guys very well. I mean I know what I want the code to do but translating that into readable English is hard for me to do as well as many others. It seems like more than helping what you guys do is try to correct the OP's behavior. That's not what they came here for. And then when a guy comes here, clearly explains his issue, and includes the code nobody is there to help if it's even a little bit complex. And by complex I mean the answer isn't easily available on Google. Because for a lot of Unity users like myself we look on google first. Heck I dread posting anything here. It's almost as if I'm ruining your guys day by asking for help. At least that's the kind of welcome I get.
     
  15. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    I appreciate that.

    Now ... given that, how do you propose to explain what you want to the computer?

    This is why we ask you process questions, encourage you to troubleshoot, learn the terminology, and we try to get you to actually improve your ability to explain what you want.

    It's not because we're "cold" (whatever that means) but rather because we don't want to sit here going around and around with somebody who cannot explain and refuses to learn how to explain.
     
    SamTheLearned likes this.
  16. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Kurt-Dekker likes this.
  17. theonerm2_unity

    theonerm2_unity

    Joined:
    Sep 7, 2018
    Posts:
    130
    Well the reason they're asking for help is because they don't know how to code it. They don't understand what they have to do. How can they explain something they don't understand? They can tell you what they want. But how is a person supposed to explain something they don't understand how to do?
     
  18. Leander_Ns

    Leander_Ns

    Joined:
    Aug 2, 2021
    Posts:
    12
    exactly where i am, i dont know how to do S*** but as i ask i just get bs in return (look on one of my former posts)
     
  19. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    Yes, the first I looked at was closed: "Closed for being lazy and argumentative."

    You seem to be making a case that the amount coding knowledge is the problem. What was highlighted above is a problem with communication of a problem. This is something that has nothing to do with programming or knowledge of Unity.

    An analogy if you will: You go to your doctor and say, "My body isn't working, what's wrong?" The doctor will tell you that there's no way they could give you a solution to your problem without further information. Rather than elaborating on the original issue you then say, also my friend has a problem, what's wrong? The doctor might say, "well you look pale but you need to ellaborate". Is what the doctor said "BS"? No. Do you need to be trained as a doctor to explain your symptoms clearly? No. At any time did the doctor say, "oh come on, can you please explain your symptoms in technical medical terms?" No.

    This kind of poor communication of a problem happens all the time here so all we can do when faced with it is to highlight the communication issue itself (your so-called BS) and that more information is needed to help further.

    You pushing your story as if someone is criticising not understanding Unity, isn't real and is not the case at all. It's about good communication of a problem or problems.

    With all that said above, I'm sorry if you feel how you do. If you communicate clearly on the problem, I'd personally spend a lot of time helping you. This isn't my day job at Unity, I'm here because I enjoy it when I solve problems.

    I will add that a lot of Unity devs just stay away from the forums because it's super hard work to help people who don't want to put effort in and eventually you just end up in the middle of threads like that. Lots of devs then just stay away permanently. Personally I stick with it because for all the difficulties, the people I'm helping are real and are struggling and maybe the knowledge in my head might help. I've been on the other end of that. The problem is always, understanding what the problem is.
     
  20. AnimalMan

    AnimalMan

    Joined:
    Apr 1, 2018
    Posts:
    1,164
    Learning to code can be frustrating and may take up to 4 or 5 years without school education before it is not at all frustrating.
    Following YouTube Tutorials is particularly frustrating because often the tutorial writer leaves out vital or important aspects to surprise the coder and create a dependence on their channel and expand their number of comments and interactions. While a forum user generally receives nothing for their help. But is expected to provide the resolution by reading through some YouTubers BS code.

    unless the mistake is glaringly obvious the lesson you should learn is to stop following that persons tutorials. Because copy and pasting other peoples code is a waste of time unless you actually understand the purpose of the presence of the code. And generally speaking how often does the tutorial writer tell you the purpose of the presence of the code.

    Best advice I can give is to code something simpler. Move a cube around: and make a basic AI using a rigidbody, add force, random range a float, vector forward, vector Right, vector up, -vector right, -vector forward -vector up and you’ll be on your way to enlightenment in no time. And you won’t be frustrated.

    unity documentation on available and provided functions will teach you everything you need to know about how to go about intelligently controlling an object.
     
  21. Leander_Ns

    Leander_Ns

    Joined:
    Aug 2, 2021
    Posts:
    12
    exactly as you said in the end of your whole paragraph of text the problem is understanding what the problem is, and that is correct. yet how should i find out the problem without knowing S***.

    Your anology is quite correct too but in my case and my friend over here @Undlien we are the uneducated doctors who are getting a very complicated "riddle" from a patient on what is wrong with them. realisticly in our case we are the uneducated programmers trying to basicly learn a new language but as we do something wrong (get error code or a response from @MelvMay or @Kurt-Dekker) with you speaking an alien language. please when we new commers to programming ask for help, speak our language and show us some basics on how your language wourks on the way. but soo far you "boomers" dont remember how it is to be a starter, and thats fine, just be repectful instead of treating us like S***.
     
  22. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,546
    No, I meant describing your problem by describing the symptoms well. Not understanding the symptoms or why they are there or the official names of the symptoms etc.

    So professional discourse has gone out the window then. This is unfortunately your problem and won't get your far here.

    I am being respectful. If you want respect then please don't attack as you just did.

    You do understand I'm trying to clarify our position and help, not argue right?
     
  23. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,974
    Closed for lazy, inappropriate use of the forums and complete failure to follow the rules. READ them, follow them. Posting again failing to follow the rules will get a warning.

    Now, pay attention... these forums are to HELP folks on their development journey. NOT REPLACE it. Slapping code in a post and crying "is broke fix it!" is completely unacceptable. If you know "nothing" than stop posting in the fourms. Got to the learn section of the site and start going through tutorials. Or take a class, or hire someone. Do not waste folks time here. There are many, many folks (and staff) who are very helpful and are willing to work through things. If you are being very lazy and crying for help at every turn for simple stuff you should be able to solve you are wasting their time that be could spent help more considerate users.

    Game development is complex and require many, many skills. If IS NOT FOR EVERYONE. If you are incapable/unwilling to grasp the very simple tasks like reading, spelling and basic research / troubleshooting, it may be time to find other ways to spend your time.
     
    Kurt-Dekker likes this.
Thread Status:
Not open for further replies.