Void sometimes and sometimes not being considered errors.

Hey, I’m still really new to Unity, and while watching one of Brackey’s tutorials on how to make a 2D platformer, an issue with the tiling script that I ran into is that out of the 9 errors I’m stuck with, a lot of them are saying that a Void, camHorizantalExtend, and even certain brackets were unexpected symbols on some lines, but they aren’t questioned at all in others. I’m really not sure as to what’s going on, and help or advice would be very appreciate.

It’s impossible to be sure without seeing your code, but the most common reason that newbies run into those sorts of errors is that they accidentally put the definitions of some functions inside of other functions.

Think of it like commas. Some places are wrong for having one, other places are wrong for not having one. But it’s easy if you know what a comma does. Computer rules are the same way.

Many of the samples are just to see what happens if you copy and change a few things, but they’re skipping completely over the “grammar” rules about what goes where and what it tells the computer.

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

[RequireComponent (typeof(SpriteRenderer))]

public class Tiling : MonoBehaviour {

    public int offsetX = 2;     //The offset so we don't get any weird errors.

    //Used for checking if we need to instantiate stuff.
    public bool hasARightBuddy = false;
    public bool hasALeftBuddy = false;

    public bool reverseScale = false; //Used if object is not tilable.

    private float spriteWidth = 0f; //The width of our element.
    private Camera cam;
    private Transform myTransform;

    void Awake (
        cam = Camera.main;
        myTransform = Transform;
    )
    // Use this for initialization
    void Start () {
        SpriteRenderer sRenderer = GetComponent<SpriteRenderer>();
        spriteWidth = sRenderer.sprite.bounds.size.x;
    }
   
    // Update is called once per frame
    void Update () {
        //Does it still need buddies? If not, do nothing.
        if (hasALeftBuddy == false || hasARightBuddy == false) (
            //Calculate the camera's extent (Half the width) of what the camera can see in world cordinates.
            float camHorizantalExtend = orthographicSize * Screen.Width/Screen.height;

            //Calculate the x position where the camera can see the edge of the sprite. (Element)
            float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth/2) - camHorizantalExtend;
            float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth/2) + camHorizantalExtend;

                // Checking if we can see the edge of the element and then calling MakeNewBuddy if we can.
            if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
       
        )
            (
                MakeNewBuddy (1);
                hasARightBuddy = true;
            )
            else if (cam.transform.x <= edgeVisiblePositionLeft + offsetX && hasALeftBuddy == false)
            (
                MakeNewBuddy (-1);
                hasALeftBuddy = true;
        )
    }
}
            //A function that creates a buddy on the side required.
            Void MakeNewBuddy (internal rightOrLeft) (
                //Calculating the new position for our new buddy

                Vector3 newPosition = new Vector3 (myTransform.platform.x + spriteWidth * rightOrLeft, myTransfrom.position.y, myTransform.position.z);
                // Initiating our new body and storing him in a variable
                Transform = newBuddy = Instantiate (myTransform, newPosition, myTransform.rotation) as Transform;

                //If not tilable, let's reverse x size of our object to get rid of ugly seems
                if (reverseScale == true) (newBuddy.localScale = new Vector3 (newBuddy.localScale.x*-1, newBuddy.localScale.y, newBuddy.localScale.z);
            )
           )

                newBuddy.parent = myTransform.parent;
                if (rightOrLeft > 0) (
                    newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
            )
                else (
                    newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
                )

Here’s the code so far.

Keep in mind capitalization is absolutely critical, and so is spelling. Even one wrong letter or wrong capitalized letter will sink your battleship. The word ‘void’ is a C# keyword, but Void has no built-in language meaning.

Also, refer to the first post in the forum to make your code readable.

1 Like

Hmm…They’re still considered errors, though. Like, on void Awake on line 21 and void Start on line 26, it’s considering void an Unexpected Symbol, but void Update on line 32 is just fine for some reason. It says the same thing about the camHorizantalExtend in line 36, and even says that certain words “Do not exist in the current context”.

Your Awake function looks like

void Awake (
    // stuff
)

but you want it to look like

void Awake() {
    // stuff
}

Parentheses () and curly braces {} each separate out a different part of the function. Parentheses are for the parameters that the function takes as input (in this case, there aren’t any, but you still need parentheses around an empty list) and the curly braces are for the function body (the instructions to execute when the function gets invoked).

I think this error in Awake is also causing your error in Start, because the compiler is still looking for the curly braces around the function body of Awake when it gets to Start.

Your MakeNewBuddy function has several problems: it’s outside of the class definition, it mixes up parentheses () and curly braces {} and it doesn’t look like they’re positioned correctly, and I’m pretty sure you didn’t want to say “internal” (probably you tried to say something else and it got “fixed” to internal because the computer misunderstood the context, because you’re trying to put the function outside of the class.)

Oooooooh, okay. I know this won’t be the last thing I ask about, but I wanna thank you guys now, because this help was heccing instantaneous, and it does mean a lot to me, since I wanna combine my art and animation skills with game development. I r e a l l y appreciate your help.

1 Like

But, anyways, fixing the brackets cleared up two of the errors, and I’m now down to 7. What could be going onto the camHorizantalExtend on line 36?

Wait, so I managed to fix the issue with 36 being read as an error, and now I’m down to 6 errors, line 44, 46, 48, 51, 53, and 58.

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

[RequireComponent (typeof(SpriteRenderer))]

public class Tiling : MonoBehaviour {

    public int offsetX = 2;     //The offset so we don't get any weird errors.

    //Used for checking if we need to instantiate stuff.
    public bool hasARightBuddy = false;
    public bool hasALeftBuddy = false;

    public bool reverseScale = false; //Used if object is not tilable.

    private float spriteWidth = 0f; //The width of our element.
    private Camera cam;
    private Transform myTransform;

    void Awake () {
        cam = Camera.main;
        myTransform = Transform;
    }
    // Use this for initialization
    void Start () {
        SpriteRenderer sRenderer = GetComponent<SpriteRenderer>();
        spriteWidth = sRenderer.sprite.bounds.size.x;
    }
   
    // Update is called once per frame
    void Update () {
        //Does it still need buddies? If not, do nothing.
        if (hasALeftBuddy == false || hasARightBuddy == false) {
            //Calculate the camera's extent (Half the width) of what the camera can see in world cordinates.
            float camHorizantalExtend = orthographicSize * Screen.Width/Screen.height;

            //Calculate the x position where the camera can see the edge of the sprite. (Element)
            float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth/2) - camHorizantalExtend;
            float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth/2) + camHorizantalExtend;

                // Checking if we can see the edge of the element and then calling MakeNewBuddy if we can.
            if (cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
            }
            (
                MakeNewBuddy (1);
                hasARightBuddy = true;
            )
            else if (cam.transform.x <= edgeVisiblePositionLeft + offsetX && hasALeftBuddy == false)
            (
                MakeNewBuddy (-1);
                hasALeftBuddy = true;
            )
    }
}

            //A function that creates a buddy on the side required.
             void MakeNewBuddy (int rightOrLeft) (
                //Calculating the new position for our new buddy

                Vector3 newPosition = new Vector3 (myTransform.platform.x + spriteWidth * rightOrLeft, myTransfrom.position.y, myTransform.position.z);
                // Initiating our new body and storing him in a variable
                Transform = newBuddy = Instantiate (myTransform, newPosition, myTransform.rotation) as Transform;

                //If not tilable, let's reverse x size of our object to get rid of ugly seems
                if (reverseScale == true) (newBuddy.localScale = new Vector3 (newBuddy.localScale.x*-1, newBuddy.localScale.y, newBuddy.localScale.z);
            )
            )

                newBuddy.parent = myTransform.parent;
                if (rightOrLeft > 0) (
                    newBuddy.GetComponent<Tiling>().hasALeftBuddy = true;
            )
                else (
                    newBuddy.GetComponent<Tiling>().hasARightBuddy = true;
                )

This is what the script looks like now for reference.

Line 44: You have a } for no apparent reason.

Lines 45 & 48: You are using () where you should have {}

Lines 50 & 53: You are using () where you should have {}

Line 58: This function is still outside the body of the class, like I said earlier. You have more () vs {} mix-ups after this point, too.

More generally, you need every punctuation mark to be the exactly correct mark in the exactly correct place or your program won’t work. Computers do not “get the gist” of your code.

Programming computers is like bargaining with evil genies: they’ll grant your wishes, but you need to be super careful about how you phrase your wishes, because they’re going to give you exactly what you asked for, not what you meant.

1 Like

Alright, fixing the brackets on 45, 48, 51, and 53 fixed those errors, but removing the bracket from Line 44 made the errors sky rocket from 3 to 24.

The number of errors you have is not informative. A single curly bracket in the wrong place can easily cause hundreds of compiler errors, despite just being one problem. Post the current version of your code, and the exact text of the error (usually, the first compiler error is the most informative as it will tell you when things started to go wrong, so assuming you don’t want to paste all 24, the first one is the best).

Also, if you are getting this many errors in a single script, you should seriously consider practicing with some smaller scripts first. You may be trying to bite off too much at once.

When you DO make a big script, you want to break it into small pieces that can be compiled and tested incrementally before you write the next piece. Even professionals do this, because even professionals make coding errors fairly frequently, and errors are easier to identify and fix when you know they are related to a small batch of new code (because the rest of your code was working an hour ago).

I am a professional and I make constantly errors constantly, not just frequently.

But I try to learn from every mistake and modify my behavior to minimize recurrence. That’s the magic sauce.

Unintentional demonstration? :wink:

1 Like

Mmmmmmmmmm…Okay, got it. I was gonna try the script over again, but this time r e a l l y paying attention to everything I type, and just getting more used to typing code in general. I’ll still keep all of this in mind, so thanks for all if your help.