Unity Scene Manager 2018 - c#

I have tried using
using UnityEngine;
using UnityEngine.SceneManagement;
public class LoadBScene : MonoBehaviour
{
void Start()
{
Debug.Log("LoadSceneB");
}
public void LoadB(string SceneB)
{
Debug.Log("sceneName to load: " + SceneB);
SceneManager.LoadScene(SceneB);
}
}
I have not been able to get the scene to change.
Anyone have any ideas?
Please and thank you!

Looks to me like the problem here is that you simply aren't calling the LoadB() method which means that SceneManager.LoadScene() is never reached. Inside the Start method just put
LoadB("InsertSceneNameHere")

Related

Why is my Unity script not able to be loaded?

I am making a vr game and I am using UnityXR to do so. I am making a script that will have two attach points for an object (pistol). These points change based on which hand is holding the object.
Here is my code:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
public class XRGrabIneractableTwoAttach : XRGrabInteractable
{
public Transform leftAttachTransform;
public Transform rightAttachTransform;
protected override void OnSelectEntered(SelectEnterEventArgs args)
{
if(args.interactableObject.transform.CompareTag("Left Hand"))
{
attachTransform = leftAttachTransform;
}
else if(args.interactableObject.transform.CompareTag("Right Hand"))
{
attachTransform = rightAttachTransform;
}
base.OnSelectEntered(args);
}
}
However, when I save my code and go into Unity, it gives me this. "The associated script can not be loaded. Please fix any compile errors and assign a valid script." I have absolutely no idea how to fix this as the code looks fine to me. Can someone please help me with this?

Make the game wait for seconds

i'm creating a sort of Five Nights at Freddy's game in Unity, using c# code. My idea was to make the animatronic jumpscare the player, and then wait for like 3 seconds and at the end make the player go back to the main menu. How can i make the game (or the code) wait for that 3 seconds before going to the menu?
Thank you
have a look at this example from the official documentation
https://docs.unity3d.com/ScriptReference/MonoBehaviour.Invoke.html
using UnityEngine;
using System.Collections.Generic;
public class ExampleScript : MonoBehaviour
{
// Launches a projectile in 2 seconds
Rigidbody projectile;
void Start()
{
Invoke("LaunchProjectile", 2.0f);
}
void LaunchProjectile()
{
Rigidbody instance = Instantiate(projectile);
instance.velocity = Random.insideUnitSphere * 5.0f;
}
}
So you can add a Restart method to your class and invoke it with 3 seconds of delay in your jumpscare function
void JumpScare()
{
Invoke("Restart", 3.0f);
}
void Restart()
{
// don't forget using UnityEngine.SceneManagement; at top of your file
SceneManager.LoadScene(0); // index of your scene in builds settings
}
You can use async await on the function you use to take the player to the main menu. You need to add the namespace "using System.Threading.Tasks". Here is an example code
using UnityEngine;
using System.Threading.Tasks;
public class Play_audio : MonoBehaviour
{
async void Start()
{
await Task.Delay(1000)
Your_fncton();
}
}
Source: https://vionixstudio.com/2021/11/01/unity-async-await/

I have tried using a public void function from a different script and error message CS1061 pops up

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

Tranform.Find Isn't Getting the Correct GameObject

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.

Why OnDrop is not called? (Unity)

I have had problems with the OnDrop method, it is that it is not called, I was reading and maybe it has something to do with a Raycaster component, but I'm not sure, I don't even have knowledge of it, if someone could explain this to me I would greatly appreciate it.
Here is my code in c # plus an image of my hierarchy in Unity2D:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
public class Drop : MonoBehaviour, IDropHandler
{
public void OnDrop(PointerEventData eventData)
{
if (eventData.pointerDrag != null)
{
Debug.Log("Aqui esta");
}
}
}
From already thank you very much.
OnDrop method is not called
To ensure the Method gets called you need to ensure that both the name and inherited class you use is correct.
As it seems you need to use override for each function and instead of IDropHandler and MonoBehaviour inherit from EventTrigger.
Example:
using UnityEngine;
using UnityEngine.EventSystems;
public class Drop : EventTrigger {
public override void OnDrop(PointerEventData eventData) {
// Check if the eventData is Null.
if (eventData.pointerDrag != null) {
Debug.Log("OnDrop called.");
}
}
}
EventTrigger

Categories