I have a basic pong game I created mostly following a tutorial but I’ve gone beyond and now I’m trying to add a menu and controls on starting a new game, which in doing so requires resetting the score.
Here is the issue. I’m trying to reset the ‘LeftScore’ and ‘RightScore’ which are sub to ‘Canvas’ in the hierarchy. The ‘Logic’ script, which is simply trying to reset the values to zero doesn’t ‘see’ them and I’m getting this error:
Assets\Scripts\Logic.cs(8,12): error CS0246: The type or namespace name 'LeftScore' could not be found (are you missing a using directive or an assembly reference?)
I'll include my entire 'Logic' script for reference..
using NUnit.Framework.Internal;
using UnityEngine;
public class Logic : MonoBehaviour
{
public PongScript pongScript;
public LeftPaddle lpaddle;
public RightPaddle rpaddle;
public LeftScore lscore;
public RightScore rscore;
public Rigidbody2D pong;
void Start()
{
pongScript.GameStart();
}
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
pongScript.GameStart();
}
if (pongScript.decr > 0)
{
pongScript.decr -= Time.deltaTime;
}
else
{
speedIncrease();
pongScript.decr = 10;
}
}
public void nextRound()
{
pongScript.speed = 0f;
lpaddle.moveSpeed = 5f;
rpaddle.moveSpeed = 5f;
pong.linearVelocity = new Vector2(pong.linearVelocityX * 0, pong.linearVelocityY * 0);
pongScript.transform.position = new Vector3(0, 0, -9);
}
public void speedIncrease()
{
pongScript.speed = pongScript.speed * pongScript.incr;
pong.linearVelocity = new Vector2(pong.linearVelocityX * pongScript.incr, pong.linearVelocityY * pongScript.incr);
if (pongScript.speed > 7)
{
lpaddle.moveSpeed += 1;
rpaddle.moveSpeed += 1;
}
}
public void newGame()
{
lscore.text = 0;
rscore.text = 0;
nextRound();
}
}
I'll keep Googling and reading, but maybe there's just something I'm missing here that I'll never find on my own.
Thanks for reading even if you have no advice,
Al
You are declaring a variable of type LeftScore
and calling it lScore
Same goes for a variable of type RightScore
called rScore
Don’t you mean to declare these as something perhaps like a Text
or TMP_Text
variable?? I infer this from your manipulation of its .text property later on.
If you actually have classes called LeftScore and RightScore, well, post them. Otherwise, not sure what else to say. This is a pretty basic compiler telling you “I dunno what you mean”
Here’s more:
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.
Look in the documentation. Every API you attempt to use is probably documented somewhere. Are you using it correctly? Are you spelling it correctly? Are you structuring the syntax correctly? Look for examples!
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.
Thanks for the quick reply, really appreciate it.
LeftScore and RightScore are gameObjects under canvas that have a text field to display the score in their respective corners.
Those scores are adjusted by another scripted called Score, which is linked to each and triggered by the pong triggering the left or right was as you would assume.
So unfortunately, I don’t have anything to really link for the LeftScore or RightScore, they are pretty basic.
I’ll try and post a snippet of one of them here:
not sure that will help much, but the RightScore is the same.
LeftScore
and RightScore
are not types. They are GameObject
names.
The class type (from the top picture) is Text
You will drag LeftScore and RightScore GameObjects into the lScore / rScore fields, and when those fields are properly typed as Text type, the reference will be allowed.
You will also need using UnityEngine.UI;
at the top of your scene.
And you can remove that NUnit line, it’s just editor junk.
You may also need to install the UGUI package.
yea, the scoring works correctly during game play, it’s set up just as you described. What I can’t do is reset the scores to zero with the “Logic” script that is attached to the “Main Camera”…
all other functions within “Logic” work perfectly, I just can’t reset the scores. I’ll attach Logic.
I figured it all out… finally…
I thought the whole program was fubar at one point but that was due to a restart of Unity, thinking that would help, and opening the wrong version.
The solution I found was creating a gameObject that I could link the script too, then after rereading Kurt-Dekker’s post, I went through it more carefully and he was correct. I changed the type to ‘Text’ but then I also realized I was trying to set text field to an int value without converting it.
All good now, thanks everyone, and thank you Kurt… appreciate the thorough explanation…
2 Likes