DialogueTrigger.Text is a field, but a type was expected+ - c#

I'm making a dialogue system in unity, in which a player would walk up to an NPC and press E to trigger a dialogue response, However, when I attempt to access the "Text" Component of the GameObject, I receive two of the same errors:
Assets/DialogueTrigger.cs(28,24): error CS0118: DialogueTrigger.Text is a field' but a `type' was expected
This is the component I am trying to access:
Text (Script)
The component is in This game object:
Game Object
Here is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueTrigger : MonoBehaviour {
public string Text;
public string Name;
public GameObject Dialog;
public GameObject Texta;
public GameObject Namea;
public Collider2D collision;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D(Collision col)
{
if (Input.GetKeyDown(KeyCode.E))
{
Dialog.SetActive(true);
Texta.GetComponent<Text>().Text = "Hello!";
Namea.GetComponent<Text>().Text = "Guy";
}
}
}
Any solutions are greatly appreciated.

There's a name conflict between your field named 'Text' and the component type 'Text', so the compiler doesn't understand which one you meant by GetComponent<Text>().
You can resolve this by changing the name of your field to something else, like for example 'text'.
You also don't even need to call GetComponent<Text>() if you change the types of your Texta and Namea fields to Text.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueTrigger : MonoBehaviour {
public string Text;
public string Name;
public GameObject Dialog;
public Text Texta;
public Text Namea;
public Collider2D collision;
void OnTriggerEnter2D(Collision col)
{
if(Input.GetKeyDown(KeyCode.E))
{
Dialog.SetActive(true);
Texta.text = "Hello!";
Namea.text = "Guy";
}
}
}

Related

Can't access variable from another script

Assets\Scripts\Wood.cs(32,9): error CS0201: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement
Well I'm trying to take the bool hasTorch and put it in the script Wood.cs to know if the player has a torch or not.
(I'm new so it's probably easy to fix, I just don't know :c)
Script 1 (Chest.cs):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chest : MonoBehaviour
{
public Sprite openChest;
public GameObject chest1;
public GameObject chestBox;
public GameObject torch;
public bool hasTorch = false;
public GameObject darkness;
public GameObject chatBox;
private void OnTriggerEnter2D(Collider2D other)
{
chest1.GetComponent<SpriteRenderer>().sprite = openChest;
torch.SetActive(true);
chatBox.SetActive(true);
darkness.SetActive(false);
chestBox.SetActive(false);
hasTorch = true;
}
public void Close()
{
chatBox.SetActive(false);
}
}
Script 1 (Wood.cs):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Wood : MonoBehaviour
{
public GameObject chestScript;
public Chest script;
public GameObject chatBox;
private void OnTriggerEnter2D(Collider2D other)
{
if (script.hasTorch == true)
{
chatBox.SetActive(true);
}
if (script.hasTorch == true)
{
chatBox.SetActive(true);
}
}
public void Close()
{
chatBox.SetActive(false);
}
void Start(){
chestScript.GetComponentInChildren<Chest>().hasTorch;
}
}
This line does not do anything (not a valid statement, as the error suggests):
chestScript.GetComponentInChildren<Chest>().hasTorch;
you could Log it or set it to true/false like this (a valid assignment):
chestScript.GetComponentInChildren<Chest>().hasTorch = false;
You have created a variable of type Chest but have not told Unity which Chest instance you want to access. Imagine you had a couple of GameObjects, all with this script attached, each with a different value for hasTorch. Unity wouldn't know which instance you have in mind, that is why you have to specifically assign the value.
All you have to do is to add this line into the Start() method:
script = someKindOfaGameObject.GetComponent<Chest>();
From now on you should be able to access all the public variables in the Chest script using script.variable syntax.

How could i fix an error (cs0120) in unity?

In unity, I'm trying to make a press of a button increase the speed of the player. However, each time I run it. It gives me:
Error CS0120: An object reference is required for the non-static
field, method, or property 'PlayerController.speed'
I've already tried changing order of the code, so what could I do?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Upgrader1 : MonoBehaviour
{
void Start()
{
GameObject Player = GameObject.Find("Player");
PlayerController PlayerController = Player.GetComponent<PlayerController>();
}
public void Upgrade1()
{
PlayerController.speed++;
}
}
public class Upgrader1 : MonoBehaviour
{
PlayerController PlayerController; //It should be member variable
void Start()
{
GameObject Player = GameObject.Find("Player");
PlayerController = Player.GetComponent<PlayerController>();
}
public void Upgrade1()
{
PlayerController.speed++;
}
}
it's always a good thing to use proper naming conventions.
PlayerController _PlayerController;
void Start() {
GameObject Player = GameObject.Find("Player");
_PlayerController = Player.GetComponent<PlayerController>();
}
public void UpgradeSpeed() // I changed the name according to its functionality
{
_PlayerController.speed++;
}
With this, you won't put PlayerController class reference again by mistake.

Unity text not updating

I'm trying to get the title/description of a playing card to change by creating a card class that holds the information:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu (menuName = "Card")]
public class Card : ScriptableObject
{
public string cardName;
public Sprite art;
public string cardDetail;
}
Then load it with another script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CardViz : MonoBehaviour
{
public Text title;
public Text detail;
public Image art;
public Card card;
private void start()
{
LoadCard(card);
}
public void LoadCard(Card c)
{
if (c == null)
{
return;
}
card = c;
title.text = c.cardName;
detail.text = c.cardDetail;
art.sprite = c.art;
}
}
I created prefab with the basic layout of a card. Then I created a new asset value in unity for a card and given it a name and detail. Then assigned it to the public valuable Card under CardViz along with the corresponding title, detail and image variable to create a new prefab but none of the text change when I drag the newly made prefab into the hierarchy. Any clue as to what I'm doing wrong here?
A small typo. Your start method needs to have a capital s.
private void Start()
{
LoadCard(card);
}

How can I link my C# scripts so that item count influences score in my Unity game?

I am trying to make it so that collecting an item will cause the score to increase. After perusing the documentation for Unity, I have written the following C# scripts. CollectableItem.cs is attached to the items in the game. ScoreBoard.cs is attached to the UI display. I'm getting the error message, "'ScoreBoard.score' is inaccessible due to its protection level." If I make the variables in ScoreBoard.cs public, I get a different error message: "An object reference is required for the non-static field, method, or property 'ScoreBoard.score'."
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollectibleItem : MonoBehaviour {
[SerializeField] private string itemName;
[SerializeField] private int pointsValue;
void OnTriggerEnter(Collider other) {
Managers.Inventory.AddItem(itemName);
Destroy(this.gameObject);
ScoreBoard.score += pointsValue;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreBoard : MonoBehaviour {
[SerializeField] private Text scoreLabel;
private int score;
void Start () {
score = 0;
}
void Update () {
scoreLabel.text = "Score: " + score.ToString();
}
}
UPDATE: Here is Take 2 on CollectibleItem.cs. Now I'm being informed that "board" does not exist in the current context...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollectibleItem : MonoBehaviour {
[SerializeField] private string itemName;
[SerializeField] private int pointsValue;
void Start() {
var uiObject = GameObject.Find("Timer");
ScoreBoard board = uiObject.GetComponent<ScoreBoard>();
}
void OnTriggerEnter(Collider other) {
Managers.Inventory.AddItem(itemName);
Destroy(this.gameObject);
board.score += pointsValue;
}
}
This is not able to work because you make a so-called static access to the ScoreBoard class. That means you try to change the variable of the ScoreBoard class. What you want to do is to change the variable on one of the instances. When the UI Object is created, an instance of the class of your ScoreBoard-Script is created. Like every item has its own instance of CollectibleItem.
You can get the instance in this way:
var uiObject = GameObject.Find("Name of UI Object");
ScoreBoard board = uiObject.GetComponent<ScoreBoard>();
You can do this in Start() and save the ScoreBoard variable in the script where your other private variables reside and use it later in the trigger, or you can do this directly in your Trigger function and directly set the score:
board.score += pointsValue;
EDIT: You have to place the declaration of Scoreboard inside the class:
ScoreBoard board;
void Start ()
...
Or put the code from start to OnTriggerEnter.

Events in scriptable objects

Is it possible to use UnityEvents and/or UnityActions in scriptable objects?
My attempt looks like this:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
[CreateAssetMenu(menuName ="Dog", fileName ="New Dog")]
public class Dog : ScriptableObject
{
public string Id;
public string Text;
public UnityEvent WhenFinished;
public GameObject Go;
}
Is it possible to make a scriptable object hold a reference to a GO in the hierarchy or have an event point to a listener in the hierarchy. Is there anyway a scriptable object can communicate with the logic in the hierarchy?
Yes and no.
You can get an SO to GO reference and the reverse, but only during runtime.
When outside runtime, you can only get a SO to GO reference, an SO created in the editor outside of runtime can't reference any game object (it wouldn't know from which scene to get the GO).
Following is the code showing how to achieve this.
Scriptable Object Code
using System;
using UnityEngine;
[CreateAssetMenu(menuName = "Dog", fileName = "New Dog")]
public class Dog : ScriptableObject {
public GameObject Go;
public static event Action<string> ScriptableToGameobjectEvent;
private void Awake() {
EventScript.GameobjectToScriptableEvent += InstanceCreated;
if (Application.isPlaying) {
ScriptableToGameobjectEvent("Dog Created");
}
}
private void InstanceCreated(GameObject go) {
Go = go;
Debug.Log(Go.name);
}
}
Game Object Code
using UnityEngine;
using System;
public class EventScript : MonoBehaviour {
public static event Action<GameObject> GameobjectToScriptableEvent;
public ScriptableObject myScriptable;
private void Awake() {
Dog.ScriptableToGameobjectEvent += DogCreated;
}
private void Start() {
myScriptable = ScriptableObject.CreateInstance<Dog>();
}
private void DogCreated(string str) {
Debug.Log(str);
GameobjectToScriptableEvent(gameObject);
}
}

Categories