I am getting the following error trying to build an editor window in Unity 2019.3.14f:
Assets\Editor\CellViewer.cs(19,40): error CS0117: 'Selection' does not contain a definition for 'activeGameObject'
I have found evidence of a single person with this specific error on the unity answer board, but there are no answers or updates in 5 years. I have reloaded the project, restarted unity, and restarted my system multiple times since this error first appeared.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class CellViewer : EditorWindow
{
private Map map;
[MenuItem ("Window/Cell Viewer")]
public static void ShowWindow() {
EditorWindow.GetWindow(typeof(CellViewer));
}
void OnGUI () {
if(Map.isInstanceActive())
{
GameObject obj = Selection.activeGameObject;
Tile tile = obj.GetComponent<Tile>();
if(tile != null) {
map = Map.Instance();
loadCell(map.getCell(tile.coordinates)));
}
}
}
}
How could this have happened, and what can I do about it?
This can happen if the classname Selection is ambiguous because you've created a class by that name in the global namespace.
To make sure you're using the correct one you can have your code be explicit by adding:
GameObject obj = UnityEditor.Selection.activeGameObject
Related
I'm currently following a tutorial about game dev on Unity (by Natty Creations) and I'm getting an error which I cannot solve (sorry I'm very new to this and I don't know how things work). the error is
Assets/Scripts/InputManager.cs(23,34): error CS1061:
'PlayerInput.OnFootActions' does not contain a definition for
'Movement' and no accessible extension method 'Movement' accepting a
first argument of type 'PlayerInput.OnFootActions' could be found (are
you missing a using directive or an assembly reference?)
I get this same error for three elements in my code, which is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class InputManager : MonoBehaviour
{
private PlayerInput playerInput;
private PlayerInput.OnFootActions onFoot;
private PlayerMotor motor;
// Start is called before the first frame update
void Awake()
{
playerInput = new PlayerInput();
onFoot = playerInput.OnFoot;
motor = GetComponent<PlayerMotor>();
}
// Update is called once per frame
void FixedUpdate()
{
//tell the player motor to move using the value from our movement action
motor.ProcessMove(onFoot.Movement.ReadValue<Vector2>());
}
private void OnEnable()
{
onFoot.Enable();
}
private void OnDisable()
{
onFoot.Disable();
}
}
Does anyone have any idea on how to solve this? Thank you in advance, please be patient as I don't have much experience! ^_^
I followed the tutorial step by step but I still get problems :(
I was trying to make a 2d Unity Latin translator, but i got a problem. How could i make that it recognises the Text and Dropdown variables already inside the "Declinatio". I need to check for the last 2 characters of this string which is SGenTextStr, and the data should be taken from the SGenText InputField. I dont know if you got a fix for this, i didnt find anything that was helpful on the internet.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class LatinDecl : MonoBehaviour
{
public static void Declinatio()
{
string SGenTextStr;
string AnsTextStr;
int StrLength;
SGenTextStr = SGenText.text;
AnsTextStr = AnsText.text;
StrLength = SGenTextStr.Length();
switch (SGenTextStr.charAt(StrLength - 2) + SGenTextStr.charAt(StrLength - 1))
{
default:
break;
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
InputField SGenText;
Dropdown DeclDrop;
InputField AnsText;
}
}
One of the things you may use a lot in Unity games is the Gameobject.GetComponent<>() method. It allows you to access values and functions from other components or scripts in your Unity Scene.
There are also an issue with declaring your variables in Update() as it will cause them to be wiped clean every frame, instead you may want to move them outside all together.
Because of that you will also have trouble with Declinatio() as it is currently static, which wont allow it to reference any non-static variables outside of it.
The last issue is that C# doesn't actually have an implementation of String.charat() however you can use String[int] to achieve the same thing.
With all those changes it would look something like this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
public class LatinDecl : MonoBehaviour
{
public InputField SGenText;
public Dropdown DeclDrop;
public InputField AnsText;
public void Declinatio()
{
string SGenTextStr;
string AnsTextStr;
int StrLength;
SGenTextStr = SGenText.GetComponent<InputField>().text;
AnsTextStr = AnsText.GetComponent<InputField>().text;
StrLength = SGenTextStr.Length;
switch (SGenTextStr[(StrLength - 2)] + SGenTextStr[(StrLength - 1)])
{
default:
break;
}
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
I have a problem that when i change scenes from the menu to the game and when i click i will get the error 'The object of type 'GameObject' has been destroyed but you are still trying to access it.' and this is happening while the menucontroller is not in the game scene, i have been working on this for the past few days to fix it but cant find how to do it. If anyone can help that would be great this kind of question has been asked alot but I cant find a solution for my problem.
We also have a similar script that has the same code except instead of sceneswitching its for interaction so when we try to interact with something we get the error
Thank you for your help.
menucontroller
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;
using API;
public class MenuController : MonoBehaviour
{
[SerializeField] private InputActionReference shoot;
[SerializeField] private InputActionReference shoot2;
public GameObject sendObject;
public GameObject sendObject2;
public float range = 1000;
void Start()
{
shoot.action.performed += Shoot;
}
async void Shoot(InputAction.CallbackContext obj)
{
RaycastHit hit;
if (Physics.Raycast(sendObject.transform.position, sendObject.transform.forward, out hit, range))
{
SceneManager.LoadScene("SampleScene");
}
}
}
The error message sounds quite self-explanatory.
You are trying to access an object that already was destroyed.
Since you load a new scene
SceneManager.LoadScene("SampleScene");
the current scene and its objects are unloaded / destroyed.
Whenever you deal with events
void Start()
{
shoot.action.performed += Shoot;
}
make sure to also remove the callback as soon as not needed/valid anymore
private void OnDestroy()
{
shoot.action.performed -= Shoot;
}
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 3 years ago.
I've been looking around for a possible solution but I don't solve It... This is the problem:
I have in a file with its class the various Ui, including the textmesh.
Another file, with its own class, retrieves and sets them via ".text"
result?
"NullReferenceException: Object reference not set to an instance of an
object Controller_RaceManager.Update () (at
Assets/[GameAssets]/Controller
[MobileCar]/Scripts.Code/Controller_RaceManager.cs:307)"
to better understand:
A) into Controller_UiManager.cs (attached to the canvas with all UIs)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI; using TMPro;
public class Controller_UiManager : MonoBehaviour
{
[System.Serializable]
public class UiSettings
{
public TextMeshProUGUI Label_Lap;
}
[SerializeField]
[HideLabel] public UiSettings UiElements;
void OnDrawGizmos()
{
var Canvas = this.gameObject;
UiElements.Label_Lap = getTheChild(Canvas, "Label.PilotLap").transform.GetComponent<TextMeshProUGUI>();
// note: getTheChild is a method that returns the child gameObject
}
}
B) Controller_RaceManager.cs, which of course handles all the values of races (It's attached to his prefab for loops and bla bla bla)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; using TMPro;
public class Controller_RaceManager : MonoBehaviour
{
[System.Serializable]
public class RaceManagerSettings
{
...
public Controller_UiManager UiManager;
}
[SerializeField]
[HideLabel] public RaceManagerSettings sets;
void OnDrawGizmos()
{
if(GameObject.Find("Ui-RaceLoop"))
{
sets.UiManager = GameObject.Find("Ui-RaceLoop").transform.GetComponent<Controller_UiManager>();
}
else
{
Debug.LogError("Controller Race Manage: Ui System not founded - Ui-Raceloop prefab is not in scene");
}
}
void Update()
{
string Label_Lap_Output = "";
if(...) { Label_Lap_Output = "FINISHED";}
else if(....) { Label_Lap_Output = "LAP XX/"+totalloopText; }
else { Label_Lap_Output = "LAP O"+pilotlapText+"/"+totalloopText; }
sets.UiManager.UiElements.Label_Lap.text = Label_Lap_Output;
}
}
here unity pauses and gives notice. If I take the break, it continues quietly ... but am I becoming stupid?
is there a reason?
note: Unity TextMeshProUGUI.text is NullReference not "why not all or some things" ... but textmeshpro.
Solved.
I didn't understand why the It was taken from other scripts but, with TextMesh, no.
So it was obvious that I was wondering about him (in part it was right! Because on the buttons it doesn't happen... the "ui controller" is taken via editor OnDrawGizmos function)
The solution:
TextMesh must be initialized in Start or Awake. If you try a taken it directly from the editor with OnDrawGizmo, even if you reset or empty the caches, will be null.
If instead, in start or awake:
void Start()
{
// find ui elements
sets.UiManager = GameObject.Find("Ui-RaceLoop").GetComponent<Controller_UiManager>();
}
The infamous works ... I have no idea why it happens, maybe they are different procedures between the two functions.
I am writing a State Manager script for Unity in C#. Everything appears to be correct but when I test it out inside Unity, all the Debug.Log lines output twice. I'm following along with a book called Learning C# by Developing Games with Unity 3D Beginner's Guide. I have studied and studied the reference and I do not see what I am doing wrong. The script is far from finished, I think, but according to the text there should only be one output per Log.
This is my ISBase for the interface.
namespace Assets.Code.Interfaces
{
public interface IStateBase
{
void StateUpdate();
void ShowIt();
void StateFixedUpdate();
}
}
This my the BeginState, there is also a PlayState, WonState, and LostState. They're all pretty much identical except for the class name, the constructor name, the Debug.Log output, and the the new SwitchState.
using UnityEngine;
using Assets.Code.Interfaces;
namespace Assets.Code.States
{
public class BeginState : IStateBase
{
private StateManager manager;
public BeginState (StateManager managerRef) //Constructor
{
manager = managerRef;
Debug.Log ("Constructing BeginState");
}
public void StateUpdate()
{
if (Input.GetKeyUp (KeyCode.Space))
manager.SwitchState (new PlayState (manager));
}
public void ShowIt()
{
}
public void StateFixedUpdate()
{
}
}
}
And here is the actual StateManager.
using UnityEngine;
using Assets.Code.States;
using Assets.Code.Interfaces;
public class StateManager : MonoBehaviour
{
private IStateBase activeState;
void Start ()
{
activeState = new BeginState (this);
}
void Update ()
{
if (activeState != null)
activeState.StateUpdate ();
}
public void SwitchState(IStateBase newState)
{
activeState = newState;
}
}
IF it prints twice, it means that the call is done twice.
As obvious as it may sound, it is a matter to track it down, and I believe Unity will point you to the line of code called, if you double click on the console entry in the editor. This should open your IDE, or if it is open, just point the IDE to the line called.
Based on that, you may figure out that you have the same script on multiple objects; or you may have the same debug.log call in 2 different places of the script, but that are executed close to each other, ending up with you seeing the same statement.
Not much that can be done, without put a breakpoint and follow step by step
have you checked whether you have two same scripts attached to the same game object accidentally?
usually the duplicate log occurs for me when i got same script attached twice without me noticing it.
on the other hand, i would suggest guarding input from happening when switching state. anything to do with user input will need some guard from rapid repetition.
e.g.
if (!switchingStateCoolDown) {
Input.GetKeyUp (KeyCode.Space) { ... };
}
// hope you get the idea.