C
C#11mo ago
anF

C# Code Help

trying to make a tetris clone game and I keep on getting this error CS0246 The type or namespace name 'Game' could not be found (are you missing a using directive or an assembly reference?)
9 Replies
anF
anFOP11mo ago
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Tetromino : MonoBehaviour { float fall = 0; public float fallsSpeed = 1; // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { CheckUserInput(); } void CheckUserInput() { if (Input.GetKey(KeyCode.RightArrow)) { transform.position += new Vector3(1, 0, 0); if (!CheckIsValidPosition()) { transform.position += new Vector3(-1, 0, 0); } } else if (Input.GetKey(KeyCode.LeftArrow)) { transform.position += new Vector3(-1, 0, 0); if (!CheckIsValidPosition()) { transform.position += new Vector3(1, 0, 0); } } else if (Input.GetKeyDown(KeyCode.UpArrow)) { // Try rotating transform.Rotate(0, 0, 90); // Check if the new position is valid if (!CheckIsValidPosition()) { // If not, rotate back transform.Rotate(0, 0, -90); } } else if (Input.GetKeyDown(KeyCode.DownArrow) || Mathf.Round(Time.time) - fall >= fallsSpeed) { transform.position += new Vector3(0, -1, 0); if (!CheckIsValidPosition()) { transform.position += new Vector3(0, 1, 0); } fall = Mathf.Round(Time.time); } } bool CheckIsValidPosition() { foreach (Transform mino in transform) { Vector2 pos = FindObjectOfType<Game>().Round(mino.position); if (!FindObjectOfType<Game>().CheckInsideGrid(pos)) { return false; } } return true; } } thats the code
viceroypenguin
viceroypenguin11mo ago
the error message is fairly clear. You're referencing the name Game, specifically in the CheckIsValidPosition method, but the compiler doesn't know what Game you are referring to.
TheRanger
TheRanger11mo ago
doesn't look like you defined a class called Game
viceroypenguin
viceroypenguin11mo ago
You need to either add a using to reference an existing type Game, or create a class called Game.
anF
anFOP11mo ago
thanks I will try to do that
footballjoe789
footballjoe78911mo ago
Also, in discord you can use the
`
`
key to denote that something is a code snippet Like this. You can even use a code block (three of the
`
`
key at the top and bottom of your code) to make your code look like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tetromino : MonoBehaviour
{
float fall = 0;
public float fallsSpeed = 1;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
CheckUserInput();
}

void CheckUserInput()
{
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += new Vector3(1, 0, 0);

if (!CheckIsValidPosition())
{
transform.position += new Vector3(-1, 0, 0);
}
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += new Vector3(-1, 0, 0);

if (!CheckIsValidPosition())
{
transform.position += new Vector3(1, 0, 0);
}
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
// Try rotating
transform.Rotate(0, 0, 90);

// Check if the new position is valid
if (!CheckIsValidPosition())
{
// If not, rotate back
transform.Rotate(0, 0, -90);
}
}
else if (Input.GetKeyDown(KeyCode.DownArrow) || Mathf.Round(Time.time) - fall >= fallsSpeed)
{
transform.position += new Vector3(0, -1, 0);

if (!CheckIsValidPosition())
{
transform.position += new Vector3(0, 1, 0);
}

fall = Mathf.Round(Time.time);
}
}

bool CheckIsValidPosition()
{
foreach (Transform mino in transform)
{
Vector2 pos = FindObjectOfType<Game>().Round(mino.position);

if (!FindObjectOfType<Game>().CheckInsideGrid(pos))
{
return false;
}
}

return true;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Tetromino : MonoBehaviour
{
float fall = 0;
public float fallsSpeed = 1;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
CheckUserInput();
}

void CheckUserInput()
{
if (Input.GetKey(KeyCode.RightArrow))
{
transform.position += new Vector3(1, 0, 0);

if (!CheckIsValidPosition())
{
transform.position += new Vector3(-1, 0, 0);
}
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
transform.position += new Vector3(-1, 0, 0);

if (!CheckIsValidPosition())
{
transform.position += new Vector3(1, 0, 0);
}
}
else if (Input.GetKeyDown(KeyCode.UpArrow))
{
// Try rotating
transform.Rotate(0, 0, 90);

// Check if the new position is valid
if (!CheckIsValidPosition())
{
// If not, rotate back
transform.Rotate(0, 0, -90);
}
}
else if (Input.GetKeyDown(KeyCode.DownArrow) || Mathf.Round(Time.time) - fall >= fallsSpeed)
{
transform.position += new Vector3(0, -1, 0);

if (!CheckIsValidPosition())
{
transform.position += new Vector3(0, 1, 0);
}

fall = Mathf.Round(Time.time);
}
}

bool CheckIsValidPosition()
{
foreach (Transform mino in transform)
{
Vector2 pos = FindObjectOfType<Game>().Round(mino.position);

if (!FindObjectOfType<Game>().CheckInsideGrid(pos))
{
return false;
}
}

return true;
}
}
anF
anFOP11mo ago
thanks
Sir Rufo
Sir Rufo11mo ago
$code
MODiX
MODiX11mo ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat For longer snippets, use: https://paste.mod.gg/
Want results from more Discord servers?
Add your server