ERROR: The type or namespace name 'Paddle' could not be found

Title says it all. I’ve created my ball.cs script and added this code but already there is a warning. I cannot for the life of me figure out what I’m doing wrong…

using UnityEngine;
using System.Collections;

public class Ball : MonoBehaviour {

public Paddle paddle;

// Use this for initialization
void Start () {

}

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

}

}

Couple of questions:

  1. Have you already created the Paddle.cs script?
  2. Is there a typo in the declaration of public class Paddle : MonoBehaviour {…}?
1 Like

D’oh! I had named my Paddle.cs script something else because I’d skipped ahead and tried to do things on my own, that’ll teach me! :stuck_out_tongue:

I hadn’t made the connection because I had no idea that we were in fact accessing the Paddle.cs script by doing so. I’m actually quite confused. Would you please care to break down what is actually happening by invoking this line of code?

Cheers

1 Like

In Unity you will also want / need to keep your class names and script files named the same… e.g.

paddle - paddle.cs
ball - ball.cs
block - block.cs
smurf - smurf.cs

Hope this helps…

p.s. nothing wrong with jumping ahead and trying somethings solo - its often a very good way to learn - as you have done here :slight_smile:

Which line of code? This one;

public class Ball : MonoBehaviour {

That one. So I’m sorry if I sound dumb but I guess we’re creating a public ‘something’ (not an integer, float, bool, vec, etc) and we’re somehow referring to the class ‘Paddle’ and we’re calling it ‘paddle’ in turn.

I just have trouble wrapping my head around what’s happening here exactly… :confused:

You have pretty much answered this yourself… but to explain in a little more detail…

You have three parts there;

  • public
  • Paddle
  • paddle

public is what is referred to as an Access Modifier, it is what controls what other things such as other classes and the Unity editor can see it and interact with it. There are several options but public means anything can see it.

Paddle is the Type, e.g. what sort of thing (a type) it is. So when yoy listed Int, Bool, String etc, they are also types, Paddle is just another one that you have created.

paddle is the name of the variable (field) which will refer to your type.

So, you have paddle which is a Paddle and is public.

Equally, you could rename paddle to carlsPaddle and that would be fine if it remains of type Paddle. At the moment you couldn’t say that it was of type CarlsPaddle because you haven’t written any code, e.g. a class called CarlsPaddle.

Similar examples;

We have a variable to hold the count of some items. We will call it counter. The things are whole things (not decimals/fractions) so we will use an integer. I want other parts of the code to see this variable, we will set it’s access modifier to public.

public int counter;

We have a variable to hold a message which will be displayed to a user. We will call it message. It is going to hold some words, so we will set it’s type to string. We want other parts of our code to be able to access this message, maybe changing the text, so we will set it’s access modifier to public.

public string message;

Does this help?

Hey Rob, thanks a lot for the explanation.

I guess what confused me is the fact is WASN’T working before back when my Paddle.cs script was named something else (PaddleControl.cs in fact). I had no idea that simply writing the type Paddle would it link it up to another Class. Because that’s what happened, right? I’m invoking a Paddle class (called ‘paddle’) and the only reason it worked is because of the previously created one in Paddle.cs.

Exactly, you already had a Type of Paddle, so nothing errored / died… :slight_smile:

Privacy & Terms