Unity Click and Pass WaitForSeconds - c#

I'm making simple 2d game. This is opening scene. (Logos images showing).I want to if mouse click down (0) Time passes and other image show. How can i do it ?
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class OpeningCanvasScript : MonoBehaviour
{
void Start()
{
StartCoroutine(otherImage());
}
private void Deactivate()
{
for (int i = 0; i < transform.childCount; i++)
{
transform.GetChild(i).gameObject.SetActive(false);
}
}
private IEnumerator otherImage()
{
for (int i = 0; i < transform.childCount; i++)
{
Deactivate();
transform.GetChild(i).gameObject.SetActive(true);
yield return new WaitForSeconds(3f);
// I want to if mouse click down (0) Time passes and other image show
}
SceneManager.LoadScene(2);
}
}```

I think i did it this way
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class OpeningCanvasScript : MonoBehaviour
{
bool finished = false;
void Start()
{
StartCoroutine(otherImage());
}
private void Deactivate()
{
for (int i = 0; i < transform.childCount; i++)
{
transform.GetChild(i).gameObject.SetActive(false);
}
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
finished = true;
}
}
private IEnumerator otherImage()
{
for (int i = 0; i < transform.childCount; i++)
{
finished = false;
Deactivate();
transform.GetChild(i).gameObject.SetActive(true);
StartCoroutine(waitSeconds());
yield return new WaitUntil(()=>finished);
}
SceneManager.LoadScene(2);
}
private IEnumerator waitSeconds()
{
yield return new WaitForSeconds(3f);
finished = true;
}
}

Related

Unity3D [SerializeField] Menu Array not showing

So I'm following Rugbug Redfern's tutorial on how to make Photon multiplayer but the Menu SerializeField array isn't showing. Please help.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MenuManager : MonoBehaviour
{
public static MenuManager Instance;
[SerializeField] Menu[] menus;
void Awake()
{
Instance = this;
}
public void OpenMenu(string menuName)
{
for(int i = 0; i < menus.Length; i++)
{
if(menus[i].menuName == menuName)
{
menus[i].Open();
}
else if(menus[i].open)
{
CloseMenu(menus[i]);
}
}
}
public void OpenMenu(Menu menu)
{
for(int i = 0; i < menus.Length; i++)
{
if(menus[i].open)
{
CloseMenu(menus[i]);
}
}
menu.Open();
}
public void CloseMenu(Menu menu)
{
menu.Close();
}
}

Can't make collision between enemy and player do damage to the player

using UnityEngine;
using UnityEngine.UI;
public class HealthBarController : MonoBehaviour
{
private GameObject[] heartContainers;
private Image[] heartFills;
public Transform heartsParent;
public GameObject heartContainerPrefab;
private void Start()
{
// Should I use lists? Maybe :)
heartContainers = new GameObject[(int)PlayerStats.Instance.MaxTotalHealth];
heartFills = new Image[(int)PlayerStats.Instance.MaxTotalHealth];
PlayerStats.Instance.onHealthChangedCallback += UpdateHeartsHUD;
InstantiateHeartContainers();
UpdateHeartsHUD();
}
public void UpdateHeartsHUD()
{
SetHeartContainers();
SetFilledHearts();
}
void SetHeartContainers()
{
for (int i = 0; i < heartContainers.Length; i++)
{
if (i < PlayerStats.Instance.MaxHealth)
{
heartContainers[i].SetActive(true);
}
else
{
heartContainers[i].SetActive(false);
}
}
}
void SetFilledHearts()
{
for (int i = 0; i < heartFills.Length; i++)
{
if (i < PlayerStats.Instance.Health)
{
heartFills[i].fillAmount = 1;
}
else
{
heartFills[i].fillAmount = 0;
}
}
if (PlayerStats.Instance.Health % 1 != 0)
{
int lastPos = Mathf.FloorToInt(PlayerStats.Instance.Health);
heartFills[lastPos].fillAmount = PlayerStats.Instance.Health % 1;
}
}
void InstantiateHeartContainers()
{
for (int i = 0; i < PlayerStats.Instance.MaxTotalHealth; i++)
{
GameObject temp = Instantiate(heartContainerPrefab);
temp.transform.SetParent(heartsParent, false);
heartContainers[i] = temp;
heartFills[i] = temp.transform.Find("HeartFill").GetComponent<Image>();
}
}
}
This is the HealthbarController and it works when the damage is a button. Same with heal and addheart but I can't make my enemies do the damage.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DamageScript : MonoBehaviour
{
public void Hurt(float dmg)
{
PlayerStats.Instance.TakeDamage(dmg);
}
}
and this is the damage script for my enemies. How can I make enemy do damage to the player when colliding with the player? I tried with OnCollisionEnter but whenever I put it into the code I only got a bunch of errors.
What if you tried giving your enemy the tag "Enemy" and your player a sphere collider (isTrigger checked).
And then make a take damage script for example:
public class takeDamage : MonoBehaviour
{
private int PlayerHp = 10f;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag( "Enemy")) {
PlayerHp -= 1;
Debug.Log("Hit, HP: " + PlayerHp);
//Add GUI update function here
}
}
}
and attach it to the player.
Don't forget to scale the sphere collider to the appropriate size.

How do I fix this Idle-Car Game Script

I am trying to have a game, in which everyone can buy cars (and I save that data to playerprefs). So I have 9 trails for the cars in my game and I am trying to write some code so that when you press a button the car & the trail for that car will show up.
When the button next to it is clicked, it saves that data so when people restart the game, they will still have the car & trail open and won't need to press the button again.
Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public Button[] TrailLevel;
public GameObject[] Cars, Trails;
public Text text;
public int CurrentCarToSpawn = 0;
private void Start()
{ }
private void FixedUpdate()
{
UpdateCar();
}
public void InstantiateCar()
{
TrailLevel[CurrentCarToSpawn].gameObject.active = false;
MineLevel[CurrentCarToSpawn+1].interactable = true;
PlayerPrefs.SetInt("TrailCountA", PlayerPrefs.GetInt("TrailCountA") + 1);
PlayerPrefs.Save();
CurrentCarToSpawn++;
UpdateCar();
}
void UpdateCar()
{
int TrailCountA= PlayerPrefs.GetInt("TrailCountA", 1);
for (int i = 0; i < TrailLevel.Length; i++)
{
if (i + 1 > TrailCountA)
{
TrailLevel.interactable = false;
}
if (TrailLevel.interactable)
{
Trains[CurrentCarToSpawn].gameObject.active = true;
Mines[CurrentCarToSpawn].gameObject.active = true;
}
}
text.text = PlayerPrefs.GetInt("TrailCountA").ToString();
}
}
From what I can see with your code, this is how I would approach it:
using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public Button[] TrailLevel;
public GameObject[] Cars, Trails;
public Text text;
public int CurrentCarToSpawn = 0;
private void Start()
{
// Load the current car. ADDED
CurrentCarToSpawn = PlayerPrefs.getInt("savedSelection", 0);
// Since we are loading the last selection, we need to call our
// instantiation method so it can activate the appropriate
// GameObjects.
InstantiateCar();
}
private void FixedUpdate()
{
UpdateCar();
}
public void InstantiateCar()
{
TrailLevel[CurrentCarToSpawn].gameObject.active = false;
MineLevel[CurrentCarToSpawn+1].interactable = true;
PlayerPrefs.SetInt("TrailCountA", PlayerPrefs.GetInt("TrailCountA") + 1);
// Save that this is our current selection.
PlayerPrefs.SetInt("savedSelection", CurrentCarToSpawn);
PlayerPrefs.Save();
CurrentCarToSpawn++;
UpdateCar();
}
void UpdateCar()
{
int TrailCountA= PlayerPrefs.GetInt("TrailCountA", 1);
for (int i = 0; i < TrailLevel.Length; i++)
{
if (i + 1 > TrailCountA)
{
TrailLevel.interactable = false;
}
if (TrailLevel.interactable)
{
Trains[CurrentCarToSpawn].gameObject.active = true;
Mines[CurrentCarToSpawn].gameObject.active = true;
}
}
text.text = PlayerPrefs.GetInt("TrailCountA").ToString();
}
}

Trying to access dynamic array of Public Game Objects from the inspector

I am trying to make an array of Public Game Objects variable from the inspector. I am having an issue understanding how I should be dealing with using the variable "i" as the index for the array. I get an error that says value expected, but I am not sure if I should be adding the "i" variable to my index, for example: ThingToKill[i].SetActive(true);.
The only way I can get this to work is to hard code in the number if Game Objects I want, so for instance this ThingToKill[1].SetActive(true); will allow me to affect 2 game objects.
Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class publicarray : MonoBehaviour
{
public GameObject[] ThingToKill;
public float startAfterThisManySeconds;
public float endafterThisManySeconds;
void Start()
{
ThingToKill[i].SetActive(false);
StartCoroutine("turnOn");
StartCoroutine("turnOff");
for (int i = 0; i < ThingToKill.Length; i++)
{
Debug.Log(ThingToKill[i].name);
}
}
void Update()
{
}
IEnumerator turnOn()
{
yield return new WaitForSeconds(startAfterThisManySeconds);
StartCoroutine("on");
Debug.Log("on");
}
IEnumerator turnOff()
{
yield return new WaitForSeconds(endafterThisManySeconds + startAfterThisManySeconds);
StartCoroutine("off");
Debug.Log("off");
}
IEnumerator on()
{
ThingToKill[].SetActive(true);
yield return new WaitForSeconds(.000001f);
}
IEnumerator off()
{
ThingToKill[].SetActive(false);
yield return new WaitForSeconds(.000001f);
}
}
EDIT This was the solution that worked:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class publicarray : MonoBehaviour
{
public GameObject[] ThingToKill;
public float startAfterThisManySeconds;
public float endafterThisManySeconds;
void Start()
{
for (int i = 0; i < ThingToKill.Length; i++)
{
Debug.Log(ThingToKill[i].name);
ThingToKill[i].SetActive(false);
StartCoroutine("turnOn");
StartCoroutine("turnOff");
}
}
void Update()
{
}
IEnumerator turnOn()
{
yield return new WaitForSeconds(startAfterThisManySeconds);
StartCoroutine("on");
Debug.Log("on");
}
IEnumerator turnOff()
{
yield return new WaitForSeconds(endafterThisManySeconds + startAfterThisManySeconds);
StartCoroutine("off");
Debug.Log("off");
}
IEnumerator on()
{
for (int i = 0; i < ThingToKill.Length; i++)
{
ThingToKill[i].SetActive(true);
yield return new WaitForSeconds(.000001f);
}
}
IEnumerator off()
{
for (int i = 0; i < ThingToKill.Length; i++)
{
ThingToKill[i].SetActive(false);
yield return new WaitForSeconds(.000001f);
}
}
}
Take
ThingToKill[i].SetActive(false);
StartCoroutine("turnOn");
StartCoroutine("turnOff");
and place it inside the for-loop. It's throwing an error because i is local to the loop, which means that outside of it it's undefined. Placing it in the loop will resolve the problem:
void Start()
{
for (int i = 0; i < ThingToKill.Length; i++)
{
Debug.Log(ThingToKill[i].name);
ThingToKill[i].SetActive(false);
StartCoroutine("turnOn");
StartCoroutine("turnOff");
}
}

Unity not putting sprite in right place

So in this game when the player picks up items from the scene I'm trying to have it draw the item in their inventory. The problem is whenever the player picks up an item the sprite is drawn in the wrong area. I'm calling additem from itemid class and sending it to the inventory class. when it is sent the id goes with it and that's how it knows which item to draw. I'm trying to draw it in the next empty slot in the inventory. in the inventory class if I comment in those AddItem(0) (or any number that has an item with it) it draws the sprite in the right location. any ideas on why it is drawing the sprite in the wrong location when it is called from the itemid class
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour {
ItemDataBase database;
GameObject inventoryPanel;
GameObject slotPanel;
public GameObject inventorySlot;
public GameObject inventoryItem;
int slotAmount;
public List<Item> items = new List<Item>();
public List<GameObject> slots = new List<GameObject>();
void Start()
{
database = GetComponent<ItemDataBase>();
slotAmount = 20;
inventoryPanel = GameObject.Find("Inventory Panel");
slotPanel = inventoryPanel.transform.FindChild("Slot Panel").gameObject;
for (int i = 0; i < slotAmount; i++)
{
items.Add(new Item());
// and empty slot
slots.Add(Instantiate(inventorySlot));
slots[i].GetComponent<Slot>().id = i;
//set parent to slot panel
slots[i].transform.SetParent(slotPanel.transform);
}
// AddItem(0);
// AddItem(1);
// AddItem(1);
// AddItem(1);
// Debug.Log(items[1].Title);
inventoryPanel.SetActive(false);
}
void Update()
{
if (Input.GetKeyDown("i") && inventoryPanel.activeSelf == false)
{
inventoryPanel.SetActive(true);
}
else if(Input.GetKeyDown("i") && inventoryPanel.activeSelf == true)
{
inventoryPanel.SetActive(false);
}
}
public void AddItem(int id)
{
inventoryPanel.SetActive(true);
Item itemToAdd = database.FetchItemByID(id);
if (itemToAdd.Stackable && checkIfItemIsInInventory(itemToAdd))
{
for (int i = 0; i < items.Count; i++)
{
if (items[i].ID == id)
{
ItemData data = slots[i].transform.GetChild(0).GetComponent<ItemData>();
data.amount++;
data.transform.GetChild(0).GetComponent<Text>().text = data.amount.ToString();
break;
}
}
}
else
{
for (int i = 0; i < items.Count; i++)
{
// in video had this set to -1
if (items[i].ID == -1)
{
items[i] = itemToAdd;
GameObject itemObj = Instantiate(inventoryItem);
itemObj.GetComponent<ItemData>().item=itemToAdd;
itemObj.GetComponent<ItemData>().amount = 1;
itemObj.GetComponent<ItemData>().slot = i;
itemObj.transform.SetParent(slots[i].transform);
itemObj.transform.position = Vector2.zero;
itemObj.GetComponent<Image>().sprite = itemToAdd.Sprite;
itemObj.name = itemToAdd.Title;
itemObj.name = itemToAdd.Title;
break;
}
}
}
}
bool checkIfItemIsInInventory(Item item)
{
for (int i = 0; i<items.Count; i++)
{
if (items[i].ID == item.ID)
{
return true;
}
}
return false;
}
void RemoveItem (int id)
{
for (int i = 0; i < items.Count; i++)
{
if (items[i].ID == id)
{
items[i] = new Item();
break;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemId : MonoBehaviour {
public int id;
public Inventory additem;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.tag == "Player")
{
Debug.Log(id);
additem.AddItem(1);
Destroy(gameObject);
}
}
}

Categories