C# Issues with setting muzzleflash inactive - c#

I'm in C# and I want my handgun Muzzleflash to be inactive after each shot. I keep getting error message:
"Error CS8025 Feature 'local functions' is not available in C# 4. Please use language version 7 or greater."
WHEN I fix this I still get compiler errors and can't run game. It would be highly appreciated if you could take a look at code below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunFire : MonoBehaviour
{
public GameObject Flash;
void Update()
{
if (GlobalAmmo.LoadedAmmo >= 1)
{
if (Input.GetButtonDown("Fire1"))
{
AudioSource gunsound = GetComponent<AudioSource>();
gunsound.Play();
Flash.SetActive(true);
StartCoroutine(MuzzleOff());
GetComponent<Animation>().Play("GunShot");
GlobalAmmo.LoadedAmmo -= 1;
}
}
IEnumerator MuzzleOff()
{
yield return new WaitForSeconds(0.01f);
Flash.SetActive(false);
}
}
}

MuzzleOff() method is mistakenly put within the scope of Update() method. Please move it out as separate method.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GunFire : MonoBehaviour
{
public GameObject Flash;
void Update()
{
if (GlobalAmmo.LoadedAmmo >= 1)
{
if (Input.GetButtonDown("Fire1"))
{
AudioSource gunsound = GetComponent<AudioSource>();
gunsound.Play();
Flash.SetActive(true);
StartCoroutine(MuzzleOff());
GetComponent<Animation>().Play("GunShot");
GlobalAmmo.LoadedAmmo -= 1;
}
}
}
IEnumerator MuzzleOff()
{
yield return new WaitForSeconds(0.01f);
Flash.SetActive(false);
}
}

Related

Score Not Updating

I have been trying to make an OnTriggerEnter Score system and it has not been updating and is showing no errors so am I doing something wrong that I dont know about here is my Code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class ScoreManger : MonoBehaviour
{
public TextMeshProUGUI MyscoreText;
private int ScoreNum;
// Start is called before the first frame update
void Start()
{
ScoreNum = 0;
MyscoreText.text = ScoreNum.ToString();
}
void update() {
MyscoreText.text = ScoreNum.ToString();
}
public void OnTriggerEnte2D(Collider2D col){
if(col.tag == "Score"){
ScoreNum += 1;
Debug.Log("It Worked");
MyscoreText.text = ScoreNum.ToString();
Destory(col.gameObject);
}
}
}
You spelled OnTriggerEnter2D wrong.
Or you haven't marked at least one of the colliders you are interacting with as a trigger.
Or you haven't attached the script to a gameobject.

Unity: Turn on one GameObject after the other with KeyPress

I have 7 game objects or more. I want to run only one gameObject after the other. Starting with Gameobject 0 --> GameObject 1 --> GameObject 2. At the moment my script activates a new object each time Space is pressed, and the old ones are not deactivated.
Thanks for your help!
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ToggleTest : MonoBehaviour
{
public GameObject[] objects;
public int objCount = 0;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
objCount += 1;
objects[objCount].SetActive(true);
}
}
}
If I understand your question correctly, try this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ToggleTest : MonoBehaviour
{
public GameObject[] objects;
public int objCount = 0;
private float smallDelay = 0.1f;
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && smallDelay <= 0)
{
objCount += 1;
objects[objCount].SetActive(true);
objects[objCount-1].SetActive(false); // this is only if you want to deactivate the previous object.
smallDelay = 0.1f;
}
smallDelay -= Time.DeltaTime;
}
}
This is not a perfect code, but because your function's if statement is one the Update() method, I think it needs a little delay. You can also change the smallDelay to a bigger number.
EDIT
This is the answer on your comment:
if (objCount == objects.Length)
{
objCount = 0;
object[objCount].setActive(false);
object[0].setActive(true);
}
add this in the if statement or somewhere else in the Update().

C# Issues when getting another script

So I have been following a Youtube tutorial for fence building and I have done everything. The only issue is now it wont work, 2 functions are saying that one of my variables pointer isn't defining them even though in the tutorial it worked, here is my script
using System.Collections;
using System;
using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
public class CreateFence : MonoBehaviour
{
public int Building = 1;
bool creating;
ShowMousePosition Pointer;
public GameObject polePrefab;
public GameObject fencePrefab;
// Start is called before the first frame update
void Start()
{
Pointer = GetComponent<ShowMousePosition>();
}
// Update is called once per frame
void Update()
{
}
void getInput()
{
if(Input.GetMouseButtonDown(0) && Building == 1)
{
startFence();
}
else if (Input.GetMouseButtonUp(0))
{
setFence();
}
else if (creating)
{
updateFence();
}
}
void startFence()
{
creating = true;
Vector3 startpos = System.Reflection.Pointer.getWorldPoint();///getWorldPoint says Pointer has no definition for it
startpos = System.Reflection.Pointer.snapPosition(startpos);///snapPosition says Pointer has no definition for it
}
void setFence()
{
creating = false;
}
void updateFence()
{
}
}
So I figured it out, it was the system reflect thing, apparently unity autocorrected me incorrectly, Apple = Microsoft Visual Studios

C# Unity Queue NullReferenceException

I am trying to create a simple dialogue system for my game in Unity. I've set up a special trigger for a Dialogue to start and the code is passing right variable but somehow it gets stuck at clearing the queue and throws a NullReferenceException.
I've seen through debugger that all variables and triggers work perfectly fine till cs:27 inside DialogueManager.cs. I've also checked the inspector to make sure everything is correctly assigned.
Dialogue class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class Dialogue
{
public string name;
[TextArea(3,10)]
public string[] sentences;
}
DialogueTrigger class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPC_DialogTrigger : MonoBehaviour
{
// Player
public Transform player;
// GameMaager to close
public GameObject Gameplay;
// Camera and Canvas to turn on
public GameObject DialogueManager;
// NPC Details
public GameObject InteractionNPCNameTextField;
public Transform interactionTransform;
public float radius = 3f;
private bool isBusy = false;
// DialogueStart
public GameObject DialogueStart;
void Start()
{
InteractionNPCNameTextField.gameObject.SetActive(false);
}
void Update()
{
float distance = Vector3.Distance(player.position, interactionTransform.position);
if (distance <= radius)
{
if (isBusy == false)
{
InteractionNPCNameTextField.gameObject.SetActive(true);
if (Input.GetKeyDown(KeyCode.E))
{
Dialogue();
Debug.Log("Started Dialogue procedure.");
}
}
}
else if (distance > radius)
{
InteractionNPCNameTextField.gameObject.SetActive(false);
}
}
public void Dialogue()
{
Gameplay.SetActive(false);
DialogueManager.SetActive(true);
DialogueStart.GetComponent<DialogueStart>().TriggerDialogue();
Debug.Log("Triggered Dialogue.");
}
void OnDrawGizmosSelected()
{
if (interactionTransform == null)
{
interactionTransform = transform;
}
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(interactionTransform.position, radius);
}
}
DialogueStart class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueStart : MonoBehaviour
{
public Dialogue dialogue;
public void TriggerDialogue()
{
FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
Debug.Log("Dialogue sent to dialogue manager.");
}
}
DialogueManager class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class DialogueManager : MonoBehaviour
{
public Text nameText;
public Text DialogueText;
private Queue<string> sentences;
public GameObject DialogueManagerUI;
void Start()
{
if (sentences == null)
{
sentences = new Queue<string>();
}
}
public void StartDialogue (Dialogue dialogue)
{
Debug.Log("Received dialogues: " + dialogue);
nameText.text = dialogue.name;
Debug.Log("Start Dialogue: " + sentences.Count);
sentences.Clear();
Debug.Log("Sentences Cleared: " + sentences);
foreach (string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if(sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
DialogueText.text = sentence;
}
void EndDialogue()
{
Debug.Log("End of conversation.");
}
private void Update()
{
if (DialogueManagerUI.activeInHierarchy)
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
else if (!DialogueManagerUI.activeInHierarchy)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
}
Visual Studio doesn't give me any errors.
Unity error code -> Screenshot
Debugger right before issue -> Screenshot2
Seems to me like the Queue is never assigned to sentences and it remains empty.
If it is so - why?
It sounds like DialogueManager.StartDialogue probably via DialogueStart.TriggerDialogue is called from somewhere in either Awake or at least before your Start was executed.
Especially
DialogueManager.SetActive(true);
lets assume the DialogueManager object is inactive at first anyway. So maybe your stuff is called before it is set to active.
This might also be due to the Start where you set
InteractionNPCNameTextField.gameObject.SetActive(false);
so any component on this GameObject might not have its Start method getting called. Maybe you referenced the wrong GameObject here?
Debugging would help to figure out in which order your methods get called.
In general my thumb rule for initializing is
Do everything that depends only on yourself in Awake
Do everything that depends on other components being setup already in Start
This way you can (almost) always be sure that stuff is already initialized when you need it.
However, these are only guesses but
The simple solution here:
You could already solve this by simply initializing the sentences right away using
private readonly Queue<string> sentences = new Queue<string>();
now it is definitely initialized even before Start or Awake get called!

Add float number to another script, c# unity [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 4 years ago.
I need help, i'am trying to add float number from a script to another script but it does not work. I'am new to c# and coding in general if somebody code fix the script and explained whats wrong with this i would be very thankful. This is the error i am getting.
"NullReferenceException: Object reference not set to an instance of an object
Resources.Die () (at Assets/Resources.cs:42)
Resources.Update () (at Assets/Resources.cs:22)"
Here is my scripts:
1st
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Resources : MonoBehaviour {
public float maxHealth = 5;
public float currentHealth;
public float ResourceCounter;
public Texture stoneIcon;
public Texture woodIcon;
void Start ()
{
currentHealth = maxHealth;
ResourceCounter = 0;
}
void Update ()
{
Die();
}
public void OnMouseOver()
{
if(Input.GetButtonDown("Fire1"))
{
Debug.Log("Loosing one health");
currentHealth = currentHealth - 1f;
}
}
public void Die()
{
if(currentHealth <= 0)
{
Destroy(gameObject);
Inventory inventory = GetComponent<Inventory>();
inventory.ResourceStone = inventory.ResourceStone + 1;
}
}
}
2nd
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour {
public float ResourceStone;
// Use this for initialization
void Start ()
{
ResourceStone = 0;
}
// Update is called once per frame
void Update () {
}
}
By looking at your code i think you never defined the inventory instance for your "resources" script. If you are using a getcomponent both the resources and the inventory script must be on the same gameobject to be found.
If what you want requires both scripts to be on other gameobjects you need a reference to the inventory in your resources script.
You can do this several way (like making a static reference and refering to it or defining inventory as a public variable and then adding the inventory reference in the unity editor)
This is how the first case would look:
First Script -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public static Inventory instance;
private void Awake()
{
instance = this;
}
public float ResourceStone;
// Use this for initialization
void Start()
{
ResourceStone = 0;
}
}
Second Script -
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Resources : MonoBehaviour
{
public float maxHealth = 5;
public float currentHealth;
public float ResourceCounter;
public Texture stoneIcon;
public Texture woodIcon;
void Start()
{
currentHealth = maxHealth;
ResourceCounter = 0;
}
void Update()
{
Die();
}
public void OnMouseOver()
{
if (Input.GetButtonDown("Fire1"))
{
Debug.Log("Loosing one health");
currentHealth = currentHealth - 1f;
}
}
public void Die()
{
if (currentHealth <= 0)
{
Destroy(gameObject);
Inventory.instance.ResourceStone++;
}
}
}
This is how the code would look if you did the second one:
first script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Inventory : MonoBehaviour
{
public float ResourceStone;
// Use this for initialization
void Start()
{
ResourceStone = 0;
}
// Update is called once per frame
void Update()
{
}
}
second script-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Resources : MonoBehaviour
{
public float maxHealth = 5;
public float currentHealth;
public float ResourceCounter;
public Inventory inventory;
public Texture stoneIcon;
public Texture woodIcon;
void Start()
{
currentHealth = maxHealth;
ResourceCounter = 0;
}
void Update()
{
Die();
}
public void OnMouseOver()
{
if (Input.GetButtonDown("Fire1"))
{
Debug.Log("Loosing one health");
currentHealth = currentHealth - 1f;
}
}
public void Die()
{
if (currentHealth <= 0)
{
Destroy(gameObject);
inventory.ResourceStone++;
}
}
}
Just take in account two things to decide if you will use the first or the second one. The first one is a static reference, that means that you can't make more that one inventory. In the second one you need to manually drag the reference in the editor. Just in case you don't know, writing ++ after an integer does the same as integer = integer + 1;
EDIT: There, that's better looking :D

Categories

Resources