Unity How to declare propertly Input Actions in Script CS0246 - c#

I was following a tutorial for scripting Input Actions in Unity
After finished coding what the tutorial told I've got this error in the declarations of Input System's stuff CS0246 The type or namespace name 'InputActions' could not be found
The code that causes the error is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class GameInput : MonoBehaviour
{
private InputActions InputActions;
private InputAction StartAction;
void Start() {
InputActions = new InputActions();
}
private void OnEnable() {
StartAction = InputActions.Start.Start;
StartAction.Enable();
InputActions.Start.Start.performed += StartGame;
InputActions.Start.Start.Enable();
}
public void StartGame(InputValue value) {
// CODE HERE
}
}
The error is caused by the declaration private InputActions InputActions;
I think it is by the variable type InputActions that may not exist.
But the tutorial also uses a type that doesen't exist.
How can I make Unity to recognize the InputActions type?

Typo: Unity does not have an InputActions Type, but it has a InputAction Type. See how you spelled the type different in your example. InputActions does not exist so you can't use it.
Change InputActions to InputAction.

Related

i cant find a way to fix this unity3d action map script error CS1061

I got this error and I can't find a way to fix it
here is the error:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class InputManager : MonoBehaviour
{
private PlayerInput playerInput;
private Playerinput.OnFootActions onFoot;
void Awake()
{
playerInput = new PlayerInput();
onFoot = playerInput.OnFoot;
}
void Update()
{
}
private void OnEnable()
{
onFoot.Enable();
}
private void OnDisable()
{
onFoot.Disable();
}
}
'PlayerInput' does not contain a definition for 'OnFoot' and no accessible extension method 'OnFoot' accepting a first argument of type 'PlayerInput' could be found (are you missing a using directive or an assembly reference?) [Assembly-CSharp]
General note: Unity InputSystem package already brings its own type called UnityEngine.InputSystem.PlayerInput.
=> Are you sure, you are referencing the correct type?
If I
replicate your settings in a new project
make sure the Generate c# class is enabled
make sure to hit Save Asset
make sure the correct PlayerInput type is referenced
then I can access
var playerInput = new PlayerInput();
var onFoot = playerInput.OnFoot;

Trying to activate an animation trigger in UNITY by a controler button press, shows multiple errors on unity, but not on compiler

I am trying to make an animation switch using a trigger parameter on UNITY, but even though the debugger doesn't show any bugs, unity shows me a list of errors and doesn't let me run the program.
The list of errors:
Here is the list of errors shown
How I configured the animator:
animator
The input:button actions
The code in C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class SVITCH : MonoBehaviour
{
public Animator Anim;
public PlayerControls controls;
void Awake()
{
Anim = GetComponent<Animator>();
controls = new PlayerControls();
controls.Control.IdleSwitch.performed += ctx => Switcher();
}
void Switcher()
{
Anim.SetTrigger("sitch");
}
void OnEnable()
{
controls.Control.Enable();
}
void OnDisable()
{
controls.Control.Disable();
}
}
new errors:new Errors
switch is a keyword, therefore cannot be a class name. C# contains reserved words that have a special meaning for the compiler. These reserved words are called keywords. Keywords cannot be used as an identifier (name of a variable, class, interface, etc.).
Try changing it to Switch.

Cannot write my code right, because of an error CS0111

I'm a beginner in programming, So i don't know how to fix this error.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ZombieScript : MonoBehaviour
{
public Transform player;
public Transform zombie;
public GameObject zombieScript;
bool canActive = false;
void Start()
{
if (canActive == false) { zombieScript.SetActive(true); }
}
void Update()
{
UnityEngine.Debug.Log(Mathf.Abs(Vector2.Distance(player.transform.position, zombieScript.transform.position)));
if (Mathf.Abs(Vector2.Distance(player.transform.position, zombieScript.transform.position)) <= 10.00000 && canActive == false)
{
zombieScript.SetActive(true);
canActive = true;
}
}
}
Unity writes:
Assets\ZombieScript.cs(5,14): error CS0101: The namespace '' already contains a definition for 'ZombieScript'
Assets\ZombieScript.cs(12,10): error CS0111: Type 'ZombieScript'
already defines a member called 'Start' with the same parameter types
Assets\ZombieScript.cs(17,10): error CS0111: Type 'ZombieScript'
already defines a member called 'Update' with the same parameter types
in advance, thank u <3
A basic structure of a C# class looks like this:
using ...
namespace CompanyXYZ.ProductABC
{
public class Entity
{
// Constructors, properties, methods & etc.
...
}
}
CS1011 compiler error will be thrown whenever it found more than one definition within the same namespace.
Namespace allows us to avoid conflict of classes with the same name by grouping them in different namespaces.
In this case, you probably have more than one ZombieScript defined under the same namespace which is not shown in your code. Therefore, kindly check if you have it defined somewhere else (within the same namespace) and if found it you're good to go!

Unity namespace error does not exist with SceneManager

I am using unity SceneManager but it gives me a namespace error that it does not exist in
using UnityEngine;
using UnityEngine.SceneManagement;
public class ManageGame : MonoBehaviour
{
bool gameHasEnded = false;
public void GameOver()
{
if (gameHasEnded == false)
{
gameHasEnded = true;
Debug.Log("GAME OVER");
Restart();
}
}
void Restart ()
{
SceneManagement.LoadSceneMode(SceneManager.GetActiveScene().name);
}
}
I read the official documentation about SceneManger and i checked the using part and it was the same.
My version is 2019.3.14.
Why does this happen?
Could you post some more information about your script? Which method of SceneManager are you trying to call and how exactly does this code look like? What's your Unity version? What are your other 'using' statements?
Without having more details we can only guess: Both the 'UnityEditor' as well as the 'UnityEngine' namespace contain a SceneManagement namespace, which might be causing some confusion.
EDIT:
Thanks for updating the question with more information. I see two issues:
1.) You are trying to call a method on a namespace (SceneManagement is a namespace not a class). Instead you want to access the class SceneManager in the SceneManagement namespace.
2.) LoadSceneMode() is not a method in SceneManager. There's an enum with that name, but no method. You want to use the method LoadScene()
So the correct line would be:
SceneManager.LoadScene(SceneManager.GetActiveScene().name);

Why am I getting error on TryGetSettings when using postprocessing v2 ? And how can I fix it?

In the editor on my Camera I added two components : Post Process Layer and Post Process Volume
On the first component the layer I changed the Layer to a layer I added PostProcessing
On the second component I set the Is Global to true and added one effect for now Depth Of Field
Now I want to change and set values to the Depth Of Field Focal Length via a script.
So I created a new script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
public class PostprocessingEffects : MonoBehaviour
{
private PostProcessVolume postProcessVolume;
private DepthOfField depthOfField;
void Start()
{
postProcessVolume = GetComponent<PostProcessVolume>();
postProcessVolume.profile.TryGetSettings(out PostProcessEffectSettings depthOfFieldSettings);
depthOfField.focalLength.value = 1f;
}
}
I'm getting error on the line :
postProcessVolume.profile.TryGetSettings(out depthOfField);
On the TryGetSettings and the error is :
The type 'PostprocessingEffects' cannot be used as type parameter 'T' in the generic type or method 'PostProcessProfile.TryGetSettings(out T)'. There is no implicit reference conversion from 'PostprocessingEffects' to 'UnityEngine.Rendering.PostProcessing.PostProcessEffectSettings'.
Tried also :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering.PostProcessing;
public class PostprocessingEffects : MonoBehaviour
{
private PostProcessVolume postProcessVolume;
private DepthOfField depthOfField;
void Start()
{
postProcessVolume = GetComponent<PostProcessVolume>();
postProcessVolume.profile.TryGetSettings(out DepthOfField depthOfField);
depthOfField.focalLength.value = 1f;
}
}
It seems you're using incorrect type.
TryGetSettings cannot take type of PostprocessingEffects
I believe you want DepthOfField try:
postProcessVolume.profile.TryGetSettings(out DepthOfField depthOfField);
of if you have your variable already defined:
private DepthOfField depthOfField;
postProcessVolume.profile.TryGetSettings(out depthOfField);
Also make sure you have depth of field effect attached to Postprocess Volume in inspector

Categories