NullReferenceExceptionwhen using Unity UIBuilder drop down menu - unity3d

I keep getting the following error :
"NullReferenceException: Object reference not set to an instance of an object"
I have copied the example code almost exactly and yet seems to keep getting this error when trying to change the choice list, here is my code for this element (sorry I'm new to unity ui):
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.SceneManagement;
using UnityEngine.Audio;
public class SettingsMenuController : MonoBehaviour
{
public DropdownField ResolutionSelect;
[SerializeField] public List<string> Resolutions = new List<string> {"Option1","Option2","Option45"};
// Start is called before the first frame update.
void Start()
{
var root = GetComponent<UIDocument>().rootVisualElement;
ResolutionSelect = root.Q<DropdownField>("ResolutionSelect");
ResolutionSelect.choices = new List<string> {"option1"};
ResolutionSelect.value = Resolutions[0];
}
}
the error occurs on the line "ResolutionSelect.choices = new List {"option1"};".
I'm trying to make a dropdown menu to change the resolution but its proving difficult, any help or advise is appreciated and thank you in advance.

Related

Unable to use TextMeshPro, only TextMesh available in Unity Code

I am trying to change the text of a textmesh pro in my unity project, so I added a TextMeshPro Text Component to my GameObject. Now, when I try to write TextMeshPro textmeshPro = GetComponent<TextMeshPro>();, I get the Error that TextMeshPro is not available in the Namespace. I used the import using TMPro;. The only thing available is TextMesh. Is this the Code equivalent to TextMeshPro?
Also, TMP_Text is not working for me. There are no suggestions form VS 2019 other than "TextMesh"
My Full Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ContextMenuInteraction : MonoBehaviour
{
public string partName;
public GameObject assignedGameObject;
void Start()
{
TextMesh Test = assignedGameObject.GetComponent<TextMesh>();
TextMeshPro textmeshPro = GetComponent<TextMeshPro>();
}
// Update is called once per frame
void Update()
{
}
public void showNearContextMenu (GameObject target)
{
//use position of game object to spawn context menu near it
}
}
My TextMeshPro Object looks like this:
The problem is the component Type. You need find the component with this Type "TextMeshProUGUI" this is the text component for UI.
Example:
TextMeshProUGUI nameField;
nameField = GetComponent<TextMeshProUGUI>();
nameField.text = "some text";
enter image description here
For me correct
I think change compiler for example Vscode or VS or rider
Rebuild the project files. Edit > Preferences > External Tools > [Regenerate Project Files] - Problem solved by #hijinxbassist in the comments below the post

How to set a button component's transition option to "none" via C# in Unity?

I need to setup my button components in Unity to look like this via C# (i.e. with "Transition" and "Navigation" set to "None").
For the Navigation, it was a little unintuitive but I eventually found that this worked:
Navigation customNav = new Navigation();
customNav.mode = Navigation.Mode.None;
myButton.navigation = customNav;
But I can't for the life of me find a equivalent for "Transition". If it's in the documentation, it must be buried under a heading I can't find. Does anyone have any idea how it can be done?
You can change the transition value by assigning a Selectable.Transition type enum value to the .transition property.
Example
using UnityEngine;
using UnityEngine.UI;
public class TestScript : MonoBehaviour
{
[SerializeField]
private Button target = null;
private void Start()
{
target.transition = Selectable.Transition.None;
}
}
Reference
https://docs.unity3d.com/2017.3/Documentation/ScriptReference/UI.Selectable-transition.html

I'm trying to figure out how to change from one static variable to another in the inspector

new game dev here. I'm not sure if this is a stupid question but I will ask it anyway. I'm trying to figure out how to change from one variable to another in the inspector. I have a few static variables in an empty game object called currencyMaster. Sorry if my question is hard to understand.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class currencyDisplay : MonoBehaviour
{
private TextMeshProUGUI textMecH;
void Start()
{
textMecH = GetComponent<TextMeshProUGUI>();
}
// Update is called once per frame
void Update()
{
//i want to change moneyPlus since all the variables are in
//currencyMaster
textMecH.text = currencyMaster.moneyPlus.ToString("0.0");
}
}
By default, Unity only serializes public fields. In order to expose a private variable to the inspector, you need to mark it with the attribute SerializeField.
[SerializeField] private TextMeshProUGUI textMecH;

Unity Editor Can't Edit Multiple Objects

I want to build a custom inspector for one of my classes... and well... I thought I would start simple ... and I still can't get it to draw the basic inspector:
My editor script is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(AbilityBluePrint))]
[CanEditMultipleObjects]
public class AbilityBluePrintEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
// Show default inspector property editor
DrawDefaultInspector();
}
}
And the class I want to edit is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[CreateAssetMenu(fileName = "New Ability BluePrint", menuName = "Ability BluePrint")]
public class AbilityBluePrint : ScriptableObject {
public AbilityName abilityName;
public Characteristic[] characteritics;
public Effect[] effects;
public float coolDown;
public Sprite icon;
public string description;
}
Any suggestions, on how to solve the "multi-object editing not supported" message I get instead of my beautiful custom editor ??
You need to use Serialized properties if like to use Multiple object edition.
[CustomEditor(typeof(AbilityBluePrint))]
[CanEditMultipleObjects]
public class AbilityBluePrintEditor : Editor
{
var AbilityName : SerializedProperty;
function OnEnable ()
{
// Setup the SerializedProperties
AbilityName = serializedObject.FindProperty ("Ability");
}
function OnInspectorGUI()
{
// Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
serializedObject.Update ();
...
This is not explained in the documentation and I think it is quite important.
Apart from the option of using Serialized Properties, if you are not able to use those, in case you have your own items, not using the automatically managed Serialized Properties, then you have to use "targets" variable instead of "target".
I noted that if you select different types of objects with no shared script, it doesn't show shared properties, so we don't need to check if the targets are all the same type, it is always one or more. Then you do whatever you want to do by hand with each of them, inside a foreach loop.
Here's a working example of the contents of OnInspectorGUI method inside an Editor class with a simple checkbox that is changed across several scripts. Hope it helps.
var myScript = (UnityTerrainWrapper)target;
var allSelectedScripts = targets;
EditorGUI.BeginChangeCheck();
var value = GUILayout.Toggle(myScript.ShowNativeTerrain, "Draw Unity Terrain");
if (EditorGUI.EndChangeCheck())
{
foreach (var script in allSelectedScripts)
((UnityTerrainWrapper)script).ShowNativeTerrain = value;
SceneView.RepaintAll();
}
DrawDefaultInspector();

Accessing functions from different scripts in unity C#

I am relatively new to unity and I'm trying to make collectibles in a game but i need to keep a tally of how many items have been collected but still have the collected item disappear. So far I have this. And yes the collectables are hairspray :p
Collection.cs
using UnityEngine;
using System.Collections;
public class Collection : MonoBehaviour {
public control controlSrc;
void OnTriggerEnter () {
controlSrc.AddScore();
killHairSpray();
}
void killHairSpray () {
Destroy(gameObject);
}
}
control.cs
using UnityEngine;
using System.Collections;
public class control : MonoBehaviour {
public int hcTot = 0;
public void AddScore () {
hcTot = hcTot + 1;
Debug.Log("Working");
}
}
I'm not sure why it isn't working but the console says;
NullReferenceException: Object reference not set to an instance of an object
Collection.OnTriggerEnter () (at Assets/Collection.cs:10)
Thanks:) this has been driving me crazy!
You most likely haven't connected anything to the controlSrc variable so it's empty. Hence the null reference exception.
In the Unity editor select the GameObject with the Collection.cs script, then in the Inspector set the controlSrc (most likely listed as "Control Src") by assigning the game object containing the control.cs script.