using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
the stuff at the top is just stuff unity adds i am just adding this because my post has too much code apparently
void Update()
{
This tells it to print hi in the console
if (Input.GetKeyDown(KeyCode.Space)) ;
{
Debug.Log("hi.");
}
}
}
hmm, may be your problem is in your code, just clear the ; element
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("hi.");
}
}
The compiler will show you a red error and also in unity editor.
If you have done my instruction but still can not do it, create a gameobject in the hierarchy and drag the script you have written to the gameobject
Related
I am making a game in Unity with dialogue boxes and have one script made for putting the dialogue box on the screen and another to have the actual text put on the dialogue box.There is one function in the first script I have to use in the second to make it work. The problem however is that when I try to reference the first script it always occur an error.
The error message is:
Assets\scripts\dialogueHolder.cs(29,18): error CS1061: 'dialogueManager' does not contain a definition for 'ShowBox' and no accessible extension method 'ShowBox' accepting a first argument of type 'dialogueManager' could be found (are you missing a using directive or an assembly reference?)
First code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class dialogueManager : MonoBehaviour
{
public GameObject dialogueBox;
public Text dialogueText;
public bool activeDialogue;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (activeDialogue == true && Input.GetKeyDown(KeyCode.Return))
{
dialogueBox.SetActive(false);
activeDialogue = false;
}
}
public void ShowBox(string lines)
{
activeDialogue = true
dialogueBox.SetActive(true);
dialogueText.text = lines;
}
}
Second code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class dialogueHolder : MonoBehaviour
{
public string dialogue;
private dialogueManager dMan;
public GameObject Manager;
// Start is called before the first frame update
void Start()
{
dMan = Manager.GetComponent<dialogueManager>();
}
// Update is called once per frame
void Update()
{
}
void OnTriggerStay2D(Collider2D other)
{
if(other.gameObject.name=="Shadow")
{
if(Input.GetKeyDown(KeyCode.Return))
dMan.ShowBox(dialogue);
}
}
}
I can clearly see that the function ShowBox I am trying to access from the first script is the problem. But I can't figure out what to do with the code to make it work. I have tried different methods to try and reference the Class dialogueManager. For an example I have tried refrencing the actual gameobject in the game or the script alone but I can't seem to get it to work. If I remove the function ShowBox the code will run but I need the ShowBox function to actually make the dialogue boxes in my game to work so that is not a solution.
"activeDialogue = true" maybe you should put a ";" after this
I feel like this is a poor question to ask, but I couldn't figure out what the issue was.
I was making a ScoreController and I wanted to make it so that a coin object, when hit, would add 100 to the score GUI. All the coins have this script inside of them:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinScoreController : MonoBehaviour
{
public void OnTriggerEnter2D(Collider2D collider)
{
transform.parent.parent.parent.Find("Canvas/ScoreUIController").CollectCoin();
}
}
I can recognize that this is a very poor way of getting an object but I can't figure out what the issue is. Here is the script that I was trying to reference:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ScoreController : MonoBehaviour
{
public static int score = 0; //Number of coins
public static GameObject scoreUI; //A gameObject for the CoinUI
// Update is called once per frame
void Start()
{
scoreUI = GameObject.Find("ScoreUI"); //Automatically sets all coins to have their coinUI GameObject equal the CoinUI UI.
}
public void CollectCoin() //Runs when a coin is collected. Makes it disapear and adds one to the counter.
{
gameObject.SetActive(false);
score += 100;
scoreUI.GetComponent<Text>().text = "Score: " + score.ToString();
}
public void KilledMonster()
{
score += 1000;
}
}
The ScoreControllerUI is inside Canvas so I don't think I have the wrong path. Can you not transform.Find() from the scene? Here is my scene:
As I am sure you can tell, I am new to this site and to Unity, so I apologize for any mistakes in the way I asked this question. Feel free to give me constructive criticism on that as well. I am using unity version 2020.3.24f1 and coding in C#. I don't know if this is relevant but I am coding using visual studio.
Sorry again for any dumb mistakes I made.
Instead of using
transform.parent.parent.parent.Find("Canvas/ScoreUIController").CollectCoin();
use
GameObject.Find("Canvas/ScoreUIController").GetComponent<ScoreController>().CollectCoin();
Suggestion: Define a public ScoreController ScoreUIController in your CoinScoreController (I'm assuming your coin is a prefab) then drag and drop the ScoreUIController into it to assign your script to our defined variable.
Now, you would be able to call
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinScoreController : MonoBehaviour
{
public ScoreController ScoreUIController;
public void OnTriggerEnter2D(Collider2D collider)
{
ScoreUIController.CollectCoin();
}
}
P.S. : Please follow tutorials and read documentation first, as I can see your code is very process heavy.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PickupCoin : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
Destroy(this.gameObject);
Debug.Log("Coin was picked up");
}
}
}
Script is assigned to randomly spawned sprites, but player cannot interact with them, and Debug is never called. This means method is never called for reasons unknown to me.
If you need additional info I can provide it.
First, just check if the player and the created sprites have a Box Collider component.
My code doesn't work. The player is supposed to respawn when a player with the tag "Player" interacts with an object with this code. Can someone help me, thanks in advance! Code ☟
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class KillPlayerLava : MonoBehaviour
{
public int Respawn;
public float Seconds;
public MonoBehaviour script;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public IEnumerator OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
yield return new WaitForSeconds(Seconds);
SceneManager.LoadScene(Respawn);
script.enabled = false;
}
}
}
Here are something which might help you find the answer, the code you posted looks fine but there can be other things causing the issue.
Does your gameobject have a 2D collider?
Does one of the objects that it's colliding with have a Rigidbody2D?
Note: Trigger events are only sent if one of the Colliders also has a Rigidbody2D attached. Trigger events are sent to disabled MonoBehaviours, to allow enabling Behaviours in response to collisions.
Source docs : MonoBehaviour.OnTriggerEnter2D(Collider2D)
Does your play have the Player tag set?
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
public class GetPrefabs : Editor
{
public GameObject[] prefabs;
// Start is called before the first frame update
void Start()
{
prefabs = (GameObject[])Resources.LoadAll("Assets/Test/Animations/");
}
// Update is called once per frame
void Update()
{
prefabs = (GameObject[])Resources.LoadAll("Assets/Test/Animations/");
}
}
First I tried to put the script inside Assets/Test/Editor but it didn't work then I moved the script to Assets/Editor but it's not working either it's never get to the break point I put on the line in the Update or in the Start.
GetPrefabs derives from Editor. The Start() and Update() are MonoBehaviour methods (i.e. Unity looks for them if you derive from MonoBehaviour). You should look at Unity docs for the Editor class and pick appropriate methods from its list - https://docs.unity3d.com/ScriptReference/Editor.html