Search Unity

Can't find whats wrong with the code in my game

Discussion in 'Getting Started' started by Alishakitty28, Aug 27, 2019.

  1. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    I keep getting errors with the code in my program apparently its with line 26
    The code is designed to pick a random string from the list and place it in my text box on screen. The code will be attached to an on click button command. So far no success. I don't understand what i'm doing wrong

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ButtonScript : MonoBehaviour
    6. {
    7.     public Text MainText;
    8.     public Button button;
    9.     public bool ButtonClick;
    10.  
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         MainText = "";
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (ButtonClick = true) ;
    22.  
    23.         { var list = new List<string> { "1", "2", "3" };
    24.             int randomIndex = Random.Range(0, list.Count);
    25.             Debug.Log(list[randomIndex]); }
    26.         Writeline = LinkedList; {in maintext};
    27.      
    28.     }
    29. }
    Please note that the strings will be full sentences in the finished program. I have only used numbers for testing purposes
     
  2. Deleted User

    Deleted User

    Guest

    Why is there a ; after LinkedList on line 26?

    Otherwise, please post the full content of the error message.
     
  3. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    Here are the errors
     

    Attached Files:

  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Like 21 needs == instead of a single =, otherwise you are assigning instead of comparing. Same line, remove the semi-colon. Not sure what you're doing with the curly braces on line 26.

    Your code editor, like VS, will display these errors as soon as you write them by the way.
     
  5. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    I have no idea what I'm doing, all I've done is watched tutorial after tutorial. I've made changes to the code but I'm still getting the same errors
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ButtonScript : MonoBehaviour
    6. {
    7.     public Text MainText;
    8.     public Button button;
    9.     public bool ButtonClick;
    10.  
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.         MainText = "";
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if (ButtonClick == true)
    22.  
    23.         { var list = new List<string> { "1", "2", "3" };
    24.             int randomIndex = Random.Range(0, list.Count);
    25.             Debug.Log(list[randomIndex]); }
    26.         Writeline = LinkedList in maintext; }
    27.      
    28.     }
    29.  
    30.  
     
  6. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Focus on learning C# instead of Unity. You're making very basic syntax errors. Unity tutorials typically assume you already know C#, since it is a very popular and standardized programming language.

    I still don't understand what you're trying to do on line 26, or why just before that you're generating a random number to only use it to write something to the console. Where is LinkedList defined? Where is maintext defined? Why is there an extra closing curly brace on that line?
     
    Last edited: Aug 27, 2019
  7. Deleted User

    Deleted User

    Guest

    Joe-Censored likes this.
  8. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    Removing the project altogether and giving up on the on it. Nothing is working and I'm frankly sick of it all. I thought making text appear in a text box was going to be a simple introduction but it clearly is impossible. Sorry if I seem moody but that's honestly because I am at this point
     
  9. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    Look, sorry for the angry response its just I've been guided from tutorial to tutorial for about a week now and nothing I've been shown tells me how to do what I want my program to do (often not even close). I'm just getting very irritated because I want to advance further with my project but I have absolutely no idea how.
     
  10. Bill_Martini

    Bill_Martini

    Joined:
    Apr 19, 2016
    Posts:
    445
    All of us feel your pain, we all have been there. Every developer has had to go through the learning process, we're not special or arrived on this planet with this knowledge built-in.

    That being said, you'll never be a developer with that attitude. It gets a lot harder before it gets easier. It never gets easy but it is highly rewarding (sometimes just on a personal level). If you're prone to giving up easily, find something else. You've made several noob mistakes right off the bat that doomed you to failure. First, complete several of the tutorials in the Learn link above and all of the scripting tutorials. This is the bare minimum before tackling something on your own. Secondly, do not cherry pick tutorials that you perceive to be relevant to the project you want to build. Unitys tutorials will give you a broad field of concepts and functions that are mostly used in every project. Completing these will give you the understanding you need to go off on your own.

    Lastly, your entire approach to do what you want is poor. Unity has an OnClick function built into it's UI button. You should be using that in conjunction with a small script. There are a lot of bad examples out there, if you understand your tools and programming concepts you don't need them.
     
    Vryken, Ryiah and JoeStrout like this.
  11. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Bill's giving you good advice; I'd listen to him. On top of that, please notice that he (and others) have advised you to "do" the tutorials. Not "watch" the tutorials.

    A huge mistake many beginners make is to sit in front of a video and watch it. That's next to useless; you can do that all day and still not understand anything. To get any value out of a tutorial, you need to sit at your computer and actually follow along, step by step, doing yourself what they are showing you in the tutorial.
     
  12. Also, you're not in the position when you can "move forward with your project". You should not have "project" at this stage. You should learn how to write proper C# basics.
    When the basics on C# is done and you're able to spot and fix a } mismatch problem on your own, then you may be ready to step one forward and follow along tutorials where you are actually doing stuff (like make a button and click and the console writes out the "hello world").
    I usually link this fundamentals for absolute beginners from Microsoft itself:
     
    JoeStrout and Joe-Censored like this.
  13. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,148
    While @Bill_Martini made some very good points there is one thing he neglected to mention and that is on the subject of time frames. If you think a week is a long time to pick up a major concept of a game engine you're way underestimating game development.

    Just getting past the basics is a process that takes months (yes, plural) not weeks and definitely not days for a beginner to game development, and it doesn't help any that user interfaces are a complex topic for even an experienced developer to tackle. It took me weeks to become comfortable with Unity's UI let alone know the basics of creating one.

    And you will feel this way many many times both as a beginner and after you've spent years at it. Game development is not an easy field nor is it a very well paying one either. Becoming a game developer is almost entirely about passion and you have to be able to maintain that passion for the months to years it takes to make a complete game.

    Absolutely nothing wrong with moving on if you decide game development isn't for you. Just about every aspect of this field can be applied to other fields if you find you enjoy one part but not the others.
     
    Last edited: Aug 27, 2019
    JoeStrout likes this.
  14. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    My suggestion is:
    1) Yes throw away your project for now
    2) Learn C# outside of Unity
    - Once you have a basic understanding of how to write classes, methods, understand references vs value types, how to write simple console applications, etc, only then move on to #3
    3) Do some very simple Unity tutorials
    - Do, not watch, do
    4) Come up with a very simple project to start working on

    You've effectively skipped directly to step 4, so of course you're having a hard time. What you're working on should be something you take up a few months from now. You don't try out driving on the freeway before first learning what the lines on the road and those silly numbers on the dash mean.
     
  15. Deleted User

    Deleted User

    Guest

  16. Alishakitty28

    Alishakitty28

    Joined:
    Jul 15, 2019
    Posts:
    22
    Hi I'd just like to apologies for going a bit haywire the other day, long story short I had been getting frustrated with it for a few days and I have had a long term head ache due to a medical condition so mixed together it didn't go too well. I'm really sorry if I caused offence in anyway. I will take everything that's been said here on board and hopefully that will help me move forward. Thank you :) As for writing ideas down in detail been there done that filled up many a note book
     
    Last edited: Sep 3, 2019
    Bill_Martini likes this.