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
Related
I got a problem Having the fungus plugin red line
But in unity editor it works normal.
It is a little problem but i want to fix it.
Can someone help?
I try to check visual studio install for VS 2019 has already got unity plugin.
And I try to use open c# project from unity editor
but it still got the problem
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Fungus;
public class TestVariablefungus : MonoBehaviour
{
public Flowchart flowchart;
public int missioncount = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void setFungusmission(int count)
{
//flowchart.SetBooleanVariable("missioncomplete", true);
missioncount= count;
flowchart.SetIntegerVariable("mission", missioncount);
}
public void setGrabMission(bool IsGrab)
{
flowchart.SetBooleanVariable("grabOnhand", IsGrab);
}
}
I found the problem myself.
That is because I put the project folder under a lot of level of folder.
Make the fungus package cannot catch the function.
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.
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 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();
Im using Unity 4.7.0 and Vuforia 5.0.10, i cannot call the IVirtualButtonEventHandler.
using UnityEngine;
using System.Collections;
public class VBEventHandler : MonoBehaviour, IVirtualButtonEventHandler
{
}
I just came across this, hope you're still using Unity & Vuforia. You need to add using Vuforia to make the call.
using UnityEngine;
using System.Collections;
using Vuforia;
Register the Virtual Button:
To add a virtual button to an image target, add the VirtualButton element and its attributes to the ImageTarget element in the .xml file.
XML Attributes:
Name - a unique name for the button
Rectangle - defined by the four corners of the rectangle in the
target's coordinate space
Enabled - a boolean indicating whether the button should be enabled
by default
Sensitivity - HIGH, MEDIUM, LOW sensitivity to occlusion
After Registering Virtual Button Code is simple then:
using UnityEngine;
using System.Collections;
using Vuforia;
public class Custom_VirtualButton : MonoBehaviour, IVirtualButtonEventHandler
{
// Use this for initialization
void Start () {
// here it finds any VirtualButton Attached to the ImageTarget and register it's event handler and in the
//OnButtonPressed and OnButtonReleased methods you can handle different buttons Click state
//via "vb.VirtualButtonName" variable and do some really awesome stuff with it.
VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour>();
foreach (VirtualButtonBehaviour item in vbs)
{
item.RegisterEventHandler(this);
}
}
// Update is called once per frame
void Update () {
}
#region VirtualButton
public void OnButtonPressed(VirtualButtonAbstractBehaviour vb)
{
Debug.Log("Helllllloooooooooo");
}
public void OnButtonReleased(VirtualButtonAbstractBehaviour vb)
{
Debug.Log("Goooooodbyeeee");
}
#endregion //VirtualButton
}