I wanted to add multiplayer to my game but i quickly faced many many problems, i cant find any clear solution online. i made a weapon script where the player can shoot and reload.
problems:
when i shoot, on 1 screen both players shoot, but at the other screen nothing happens
when i swap weapons, on 1 screen both players swap weapons, on the second screen nothing happens
note: im still a complete beginner so go easy on me please :)) thanks!
weapon script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
using Photon.Pun;
public class weaponScript : MonoBehaviour
{
public weaponSO weaponStats;
public GameObject reloadingText;
public TMP_Text ammoValue;
public Transform firePoint;
public GameObject bulletPrefab;
public float bulletForce = 2f;
public bool isShooting;
public float pistolDamage;
public int currentAmmoClip = 1;
public int maxAmmoClip;
public int currentReserve = 1;
public int maxReserve;
public float reloadTime = 2f;
public bool isReloading = false;
public int bulletsShot;
public float startingDamage;
PhotonView view;
public void Start()
{
view = GetComponent<PhotonView>();
if(view.IsMine)
{
view.RPC("Update", RpcTarget.AllBuffered);
}
WeaponStats();
currentAmmoClip = maxAmmoClip;
currentReserve = maxReserve;
bulletsShot = maxAmmoClip - currentAmmoClip;
isShooting = false;
weaponStats.damage = startingDamage;
}
[PunRPC]
public void Update()
{
ammoValue.text = currentAmmoClip.ToString("0") + "/" + currentReserve.ToString("0");
if (Input.GetKeyDown(KeyCode.R))
{
if(currentReserve <= 0)
{
return;
}
if (currentAmmoClip == maxAmmoClip && !isReloading)
{
return;
}
else
{
StartCoroutine(Reload());
return;
}
}
if(Input.GetButtonDown("Fire1") && currentAmmoClip >= 1)
{
if(isReloading == false)
{
bulletsShot += 1;
currentAmmoClip -= 1;
isShooting = true;
Shoot();
FindObjectOfType<AudioManager>().Play("shot1");
return;
}
}
if(isReloading)
return;
if(currentAmmoClip <= 0 && currentReserve >= 1)
{
StartCoroutine(Reload());
return;
}
}
IEnumerator Reload()
{
reloadingText.SetActive(true);
isReloading = true;
yield return new WaitForSeconds(reloadTime);
if(currentReserve <= bulletsShot)
{
currentAmmoClip += currentReserve;
currentReserve = 0;
}
else
{
currentReserve -= bulletsShot;
currentAmmoClip = maxAmmoClip;
}
bulletsShot = 0;
reloadingText.SetActive(false);
isReloading = false;
}
void Shoot()
{
GameObject bullet = Instantiate(bulletPrefab, firePoint.position, firePoint.rotation);
Rigidbody2D rb = bullet.GetComponent<Rigidbody2D>();
rb.AddForce(firePoint.up * bulletForce, ForceMode2D.Impulse);
}
public void WeaponStats()
{
pistolDamage = weaponStats.initialDamage;
maxAmmoClip = weaponStats.maxAmmoClip;
maxReserve = weaponStats.maxReserve;
reloadTime = weaponStats.reloadTime;
bulletForce = weaponStats.bulletForce;
startingDamage = weaponStats.initialDamage;
}
}
weapon holder:
using UnityEngine;
public class WeaponHolder : MonoBehaviour
{
public int selectedWeapon = 0;
void Start()
{
SelectWeapon();
}
void Update()
{
if(FindObjectsOfType<weaponScript>()[0].isReloading == true)
{
return;
}
else
{
switchWeapon();
}
}
public void switchWeapon()
{
int previousSelectedWeapon = selectedWeapon;
if(Input.GetAxis("Mouse ScrollWheel") > 0f)
{
if(selectedWeapon >= transform.childCount - 1)
{
selectedWeapon = 0;
}
else
{
selectedWeapon++;
}
}
if(Input.GetAxis("Mouse ScrollWheel") < 0f)
{
if(selectedWeapon <= 0)
{
selectedWeapon = transform.childCount - 1;
}
else
{
selectedWeapon--;
}
}
if(previousSelectedWeapon != selectedWeapon)
{
SelectWeapon();
}
}
public void SelectWeapon()
{
int i = 0;
foreach (Transform weapon in transform)
{
if(i == selectedWeapon)
{
weapon.gameObject.SetActive(true);
}
else
{
weapon.gameObject.SetActive(false);
}
i++;
}
}
}
i tried adding
if(view.IsMine == true)
{
Debug.Log("true");
}
else
{
Debug.Log("false");
}
but nothing happened and i couldnt shoot anymore. thanks for reading
The problem is you don't sync anything in your game. This means the other computer can't know what it has to do.
Whenever you want to sync something you have to call an RPC function.
Maybe watch some tutorials about how to do this properly and how to set up a simple multiplayer lobby.
EDIT:
What the other comments above mentioned with .IsMine is true, but you're doing it wrong. You have to call this in start or awake and whenever the view is not yours you disable the camera of the person only and disable shooting. Its important to only do this on your computer! If you are not doing everyone will shoot when you press Shoot and everyone will walk when you walk.
Related
I am working on a 2D, topdown, roguelike game but i got stuck when i tried to make a shooting system.
when i run it and press the arrow keys, the bullet spawns and points in the right direction. It juste doesn't move in any way.
This is my code and i hope someone can figure it out. Btw, everything is connected. The player and the bullet prefab.
using System.Collections;
using System.Linq;
using UnityEngine;
namespace Assets.Scripts
{
[RequireComponent(typeof(Player))]
public class PlayerShootController : ShootControllerBase
{
[SerializeField]
private int _numberOfBombs;
public int NumberofBombs
{
get { return _numberOfBombs; }
set { _numberOfBombs = value; }
}
[SerializeField]
private PlayerHeadController _headObject;
public PlayerHeadController HeadObject
{
get { return _headObject; }
set { _headObject = value; }
}
[SerializeField]
private Bomb _bombPrefab;
public Bomb BombPrefab
{
get { return _bombPrefab; }
set { _bombPrefab = value; }
}
public bool IsShooting { get; private set; }
private KeyCode _shootKey;
private Vector2 _shootDirection;
private Player _player;
public override void Start()
{
base.Start();
_player = GetComponent<Player>();
}
public override void Update()
{
base.Update();
if (IsShooting)
{
SetHeadDirection(_shootKey);
return;
}
if (InputHelpers.IsAnyKey(KeyCode.UpArrow, KeyCode.DownArrow, KeyCode.LeftArrow, KeyCode.RightArrow))
{
if (Input.GetKey(KeyCode.UpArrow))
{
_shootDirection = new Vector2(0, BulletSpeed);
_shootKey = KeyCode.UpArrow;
}
else if (Input.GetKey(KeyCode.DownArrow))
{
_shootDirection = new Vector2(0, -BulletSpeed);
_shootKey = KeyCode.DownArrow;
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
_shootDirection = new Vector2(-BulletSpeed, 0);
_shootKey = KeyCode.LeftArrow;
}
else if (Input.GetKey(KeyCode.RightArrow))
{
_shootDirection = new Vector2(BulletSpeed, 0);
_shootKey = KeyCode.RightArrow;
}
StartCoroutine(Shoot());
}
if (NumberofBombs > 0 && InputHelpers.IsAnyKeyDown(KeyCode.LeftShift, KeyCode.RightShift, KeyCode.E))
{
var bomb = (Bomb) Instantiate(BombPrefab);
bomb.transform.position = transform.position;
NumberofBombs--;
}
}
IEnumerator Shoot()
{
IsShooting = true;
while (Input.GetKey(_shootKey))
{
var bullet = (Rigidbody2D)Instantiate(BulletPrefab);
bullet.GetComponent<BulletScript>().Shooter = transform.gameObject;
bullet.transform.position = transform.position;
if (_shootDirection.y > 0)
{
bullet.transform.Rotate(0, 0, -90);
}
else if (_shootDirection.y < 0)
{
bullet.transform.Rotate(0, 0, 90);
}
else if (_shootDirection.x > 0)
{
TransformHelpers.FlipX(bullet.gameObject);
}
bullet.AddForce(_shootDirection);
bullet.AddForce(_player.GetComponent<Rigidbody2D>().GetPointVelocity(_player.transform.position) * 0.02f);
if (ShootClips.Any())
{
var clipToPlay = ShootClips[Random.Range(0, ShootClips.Count)];
clipToPlay.pitch = Random.Range(MinShootPitch, MaxShootPitch);
clipToPlay.Play();
}
yield return new WaitForSeconds(ShootingSpeed);
}
IsShooting = false;
//Reset head flipping
if (_headObject.transform.localScale.x < 0)
{
TransformHelpers.FlipX(_headObject.gameObject);
}
}
private void SetHeadDirection(KeyCode shootKey)
{
switch (shootKey)
{
case KeyCode.UpArrow:
_headObject.SetHeadDirection(PlayerHeadController.HeadDirection.Up);
break;
case KeyCode.DownArrow:
_headObject.SetHeadDirection(PlayerHeadController.HeadDirection.Down);
break;
case KeyCode.LeftArrow:
_headObject.SetHeadDirection(PlayerHeadController.HeadDirection.Left);
break;
case KeyCode.RightArrow:
_headObject.SetHeadDirection(PlayerHeadController.HeadDirection.Right);
break;
}
}
}
}
try this:
bullet.AddForce(_shootDirection*moveSpeed*Time.deltaTime);
instead of this:
bullet.AddForce(_shootDirection);
bullet.AddForce(_player.GetComponent<Rigidbody2D>().GetPointVelocity(_player.transform.position) * 0.02f);
tweak the speed for desired result, and if you want the bullet to follow the player you will need to update the shoot direction every certain time with a couroutine
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class loadingcolorful : MonoBehaviour
{
public float speed = 0.0f;
public bool fillAmount = false;
public bool rotate = false;
public bool changeRotationDir = false;
public bool changeFillDir = false;
private RectTransform rectTransform;
private Image imageComp;
// Use this for initialization
void Start()
{
imageComp = GetComponent<RectTransform>().GetComponent<Image>();
rectTransform = transform.parent.gameObject.transform.GetComponent<RectTransform>();
}
// Update is called once per frame
void Update()
{
if (fillAmount == true)
{
if (imageComp.fillAmount != 1f)
{
if (changeFillDir == true)
{
imageComp.fillAmount -= speed;
}
else
{
imageComp.fillAmount += speed;
}
}
else
{
imageComp.fillAmount = 0f;
}
}
else
{
if (imageComp.fillAmount == 0f)
{
imageComp.fillAmount = 0f;
}
else
{
imageComp.fillAmount = 1f;
}
}
if (rotate == true)
{
if (changeRotationDir == true)
{
rectTransform.Rotate(new Vector3(0, 0, -1));
}
else
{
rectTransform.Rotate(new Vector3(0, 0, 1));
}
}
}
}
The problem is at this place :
if (changeFillDir == true)
{
imageComp.fillAmount -= speed;
}
When it will get to 0 it will not continue to fill the image to the other direction just the image will stay empty. My guess is that the values of the FillAmount ragne is between 1 and 0.
Is there a way to make it happen ?
It's a bit of a hack, but you can switch to a negative scale value and start increasing the fillAmout again. This will make it fill in the other direction, taking the 0 point as pivot.
Assuming you are filling horizontally, flipping the scale would look something like this:
transform.localScale = new Vector3(-1, 1, 1);
A working solution like I wanted :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class loadingcolorful : MonoBehaviour
{
public float speed = 0.0f;
public bool fillAmount = false;
public bool rotate = false;
public bool changeRotationDir = false;
public bool changeFillDir = false;
public bool useBackGroundImage = true;
private RectTransform rectTransform;
private Image imageComp;
private Image backGroundImage;
// Use this for initialization
void Start()
{
imageComp = GetComponent<RectTransform>().GetComponent<Image>();
backGroundImage = transform.parent.gameObject.transform.GetComponent<Image>();
UseBackgImage();
}
// Update is called once per frame
void Update()
{
UseBackgImage(useBackGroundImage);
if (fillAmount == true)
{
if (imageComp.fillAmount != 1f)
{
if (changeFillDir == true)
{
if (imageComp.fillAmount == 0)
{
imageComp.fillAmount = 1f;
}
imageComp.fillAmount -= speed;
}
else
{
imageComp.fillAmount += speed;
}
}
else
{
imageComp.fillAmount = 0f;
}
}
else
{
if (imageComp.fillAmount == 0f)
{
imageComp.fillAmount = 0f;
}
else
{
imageComp.fillAmount = 1f;
}
}
if (rotate == true)
{
if (changeRotationDir == true)
{
rectTransform.Rotate(new Vector3(0, 0, -1));
}
else
{
rectTransform.Rotate(new Vector3(0, 0, 1));
}
}
}
private void UseBackgImage()
{
if (useBackGroundImage == true)
{
backGroundImage.enabled = true;
rectTransform = transform.parent.gameObject.transform.GetComponent<RectTransform>();
}
else
{
backGroundImage.enabled = false;
rectTransform = transform.GetComponent<RectTransform>();
}
}
private void UseBackgImage(bool UseBackGroundImage)
{
if (useBackGroundImage == true)
{
backGroundImage.enabled = true;
}
else
{
backGroundImage.enabled = false;
}
}
}
I am working on setting up a simple little AI for a character in my game. For some reason I am having major problems getting animations to play while using the NavMeshAgent and I do not understand why. This is a waypoint system that I pooled off Unity API, and I can't even seem to get this working. I am hoping that if someone can give me some input on this if might clear some other things up as well. I am really lost here and would appreciate any input. The code on the bottom all works until it hits Patrol and then the player moves without animating. I feel like there is something more i need to know about navmesh's maybe. Or a lot more I need to know about programming in general.
e// Patrol.cs
using UnityEngine;
using UnityEngine.AI;
using System.Collections;
public class Enemy_Patrol : MonoBehaviour
{
public Transform[] points;
public Animator anim;
private int destPoint = 0;
private NavMeshAgent agent;
void Start()
{
agent = GetComponent<NavMeshAgent>();
anim = GetComponent<Animator>();
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
agent.autoBraking = false;
GotoNextPoint();
}
void GotoNextPoint()
{
// Returns if no points have been set up
if (points.Length == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].position;
anim.SetBool("WalkForwards", true);
anim.SetBool("IsIdle", false);
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Length;
}
void Update()
{
// Choose the next destination point when the agent gets
// close to the current one.
if (!agent.pathPending && agent.remainingDistance < 0.5f)
GotoNextPoint();
}
}
// Code i wrote to handle following chasing etc.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AI_Tester : MonoBehaviour
{
private float patrolSpeed = .2f;
public NavMeshAgent agent;
public GameObject[] waypoints;
private int waypointInd = 0;
public Transform player;
static Animator anim;
public SealForce_DestructableObjects destructableObjects;
public Transform enemy;
// Use this for initialization
void Start()
{
anim = GetComponentInChildren<Animator>();
agent = GetComponent<NavMeshAgent>();
destructableObjects = GetComponent<SealForce_DestructableObjects>();
waypoints = GameObject.FindGameObjectsWithTag("waypoints");
waypointInd = Random.Range(0, waypoints.Length);
}
void AIMovements()
{
if (Vector3.Distance(player.position, this.transform.position) <= 30)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.1f);
if (direction.magnitude > 15)
{
MoveToFiringRange();
}
if (direction.magnitude <= 15)
{
AttackPlayer();
}
}
else if (Vector3.Distance(player.position, this.transform.position) > 30)
{
WaitingOnAction();
}
}
public void Update()
{
AIMovements();
}
public void AttackPlayer()
{
anim.SetTrigger("IsAttacking");
anim.SetBool("IsIdle", false);
anim.SetBool("RunForwards", false);
}
public void MoveToFiringRange()
{
this.transform.Translate(0, 0, 0.04f);
anim.SetBool("RunForwards", true);
anim.SetBool("IsIdle", false);
anim.ResetTrigger("IsAttacking");
}
public void WaitingOnAction()
{
anim.SetBool("IsIdle", true);
anim.SetBool("RunForwards", false);
StartCoroutine("BackToPatrol");
}
//program works fine all the up to here. The only thing wrong with patrol is no animation.
IEnumerator BackToPatrol()
{
yield return new WaitForSeconds(5);
anim.SetBool("IsIdle", false);
Patrol();
}
public void Patrol()
{
Debug.Log("In Patrol");
agent.speed = patrolSpeed;
if (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) >= 2)
{
agent.SetDestination(waypoints[waypointInd].transform.position);
anim.SetBool("WalkForwards", true);
}
if (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) <= 2)
{
waypointInd += 1;
if (waypointInd > waypoints.Length)
{
waypointInd = 0;
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AI_Tester : MonoBehaviour
{
public bool isPatrolling;
private float patrolSpeed = 1.5f;
public NavMeshAgent agent;
public GameObject[] waypoints;
private int waypointInd = 0;
public Transform player;
static Animator anim;
//public SealForce_DestructableObjects destructableObjects;
//public Transform enemy;
// Use this for initialization
void Start()
{
anim = GetComponentInChildren<Animator>();
agent = GetComponent<NavMeshAgent>();
//destructableObjects = GetComponent<SealForce_DestructableObjects>();
waypoints = GameObject.FindGameObjectsWithTag("waypoints");
waypointInd = Random.Range(0, waypoints.Length);
}
void AIMovements()
{
if (Vector3.Distance(player.position, this.transform.position) <= 30)
{
Vector3 direction = player.position - this.transform.position;
direction.y = 0;
this.transform.rotation = Quaternion.Slerp(this.transform.rotation,
Quaternion.LookRotation(direction), 0.1f);
if (direction.magnitude > 15)
{
//StopCoroutine("BackToPatrol");
StopCoroutine("Patrol");
isPatrolling = false;
MoveToFiringRange();
}
if (direction.magnitude <= 15)
{
//StopCoroutine("BackToPatrol");
StopCoroutine("Patrol");
isPatrolling = false;
AttackPlayer();
}
}
else if (Vector3.Distance(player.position, this.transform.position) > 30 && !isPatrolling)
{
//StopCoroutine("BackToPatrol");
StopCoroutine("Patrol");
WaitingOnAction();
}
}
public void Update()
{
AIMovements();
}
public void AttackPlayer()
{
anim.SetTrigger("IsAttacking");
anim.SetBool("IsIdle", false);
anim.SetBool("RunForwards", false);
}
public void MoveToFiringRange()
{
this.transform.Translate(0, 0, 0.04f);
anim.SetBool("RunForwards", true);
anim.SetBool("IsIdle", false);
anim.ResetTrigger("IsAttacking");
}
public void WaitingOnAction()
{
anim.SetBool("IsIdle", true);
anim.SetBool("RunForwards", false);
StartCoroutine("BackToPatrol");
}
IEnumerator BackToPatrol()
{
isPatrolling = true;
yield return new WaitForSeconds(5);
anim.SetBool("IsIdle", false);
yield return StartCoroutine ("Patrol");
isPatrolling = false;
}
IEnumerator Patrol()
{
Debug.Log("In Patrol");
agent.speed = patrolSpeed;
agent.SetDestination(waypoints[waypointInd].transform.position);
anim.SetBool("WalkForwards", true);
while (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) >= 2 && !isPatrolling)
{
yield return null;
}
waypointInd++;
if (waypointInd >= waypoints.Length) waypointInd = 0;
}
/* public void Patrol()
{
Debug.Log("In Patrol");
agent.speed = patrolSpeed;
if (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) >= 2)
{
agent.SetDestination(waypoints[waypointInd].transform.position);
anim.SetBool("IsIdle", false);
anim.SetBool("WalkForwards", false);
}
if (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) <= 2)
{
waypointInd += 1;
if (waypointInd > waypoints.Length)
{
waypointInd = 0;*/
}
You are calling WaitingOnAction on Update every frame, which will set the animator's IsIdle back to true and start a new BackToPatrol coroutine. This should not happen. Try to check if the character has reached it's destination before calling WaitingOnAction again. Something like:
else if (Vector3.Distance(player.position, this.transform.position) > 30 && !isPatrolling)
{
WaitingOnAction();
}
And in your Coroutine:
IEnumerator BackToPatrol()
{
isPatrolling = true;
yield return new WaitForSeconds(5);
anim.SetBool("IsIdle", false);
yield return StartCoroutine("Patrol");
isPatrolling = false;
}
IEnumerator Patrol()
{
Debug.Log("In Patrol");
agent.speed = patrolSpeed;
agent.SetDestination(waypoints[waypointInd].transform.position);
anim.SetBool("WalkForwards", true);
agent.isStopped = false;
while (Vector3.Distance(this.transform.position, waypoints[waypointInd].transform.position) >= 2 && isPatrolling)
{
yield return null;
}
agent.isStopped = true;
anim.SetBool("WalkForwards", false);
waypointInd++;
if(waypointInd >= waypoints.Length) waypointInd = 0;
}
I didn't test it, but something like this should work.
So I was wondering why this happens... https://gyazo.com/51e7951c85992d7f8851462e59da65da Gif should make clear what's wrong.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class GrowlitheShieldSpell : MonoBehaviour {
public GameObject Shield;
public float coolDown = 10;
public float coolDownTimer;
public float EffectcoolDown = 5;
public float EffectcoolDownTimer;
public bool ShieldActive;
GameObject ShieldClone;
public GameObject Growlithe;
public GameObject BuffEffectcoolDowngo;
public Text BuffEffectcoolDownText;
public GameObject ShieldBuffIcon;
void Start(){
ShieldActive = false;
}
void Update () {
if (coolDownTimer < 0) {
coolDownTimer = 0;
}
if (EffectcoolDownTimer < 0) {
EffectcoolDownTimer = 0;
}
if (coolDownTimer > 0) {
coolDownTimer -= Time.deltaTime;
}
if (coolDownTimer == 0){
if (Input.GetKeyDown(KeyCode.Z)){
if (ShieldActive == false){
//when effect starts
ShieldClone = Instantiate(Shield, transform.position, Quaternion.identity)as GameObject;
ShieldClone.SetActive(true);
coolDownTimer = coolDown;
ShieldActive = true;
}}}
//when effect is over
if (EffectcoolDownTimer == 0) {
Shield.SetActive (false);
ShieldActive = false;
EffectcoolDownTimer = EffectcoolDown;
Destroy(ShieldClone);
ShieldBuffIcon.SetActive (false);
}
if (ShieldActive == true) {
EffectcoolDownTimer -= Time.deltaTime;
ShieldClone.transform.position = Growlithe.transform.position;
BuffEffectcoolDownText.text = EffectcoolDownTimer.ToString();
ShieldBuffIcon.SetActive (true);
}
}
}
I hope the cod makes it more clear... It's just very weird that the object gets removed while I never asked for it in any way inside the script. Any help?
Im not really sure whats wrong, I'm pretty inexperienced with C#, and i posted this with everyone saying there isn't enough context, so Im posting the whole script. It isn't that long though, I just need this help!
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class MusicPlayer : MonoBehaviour {
public GUISkin skin;
public Song[] playlist;
public AudioClip mlgSong;
public AudioSource fastSource;
int currentSongID;
bool isPlaying;
[System.NonSerialized]
public bool fastMode = false;
string currentSongCredits;
//Song credits
float timer = 0;
float slidePosition;
float slidePositionMax = 20;
void Start() {
slidePosition = slidePositionMax;
ShuffleSongs();
audio.clip = playlist[0].clip;
currentSongID = 0;
isPlaying = audio.isPlaying;
if (!GameSettings.music) {
fastSource.Stop();
}
}
void Update() {
if ((!audio.isPlaying || GameSettings.keybinds.GetKeyDown("nextsong")) && isPlaying) {
if (currentSongID<playlist.Length-1) {
currentSongID++;
} else {
currentSongID = 0;
}
audio.clip = playlist[currentSongID].clip;
slidePosition = slidePositionMax;
Play (); //The error is here...
}
if ((!audio.isPlaying || GameSettings.keybinds.GetKeyDown("lastsong")) && isPlaying) {
if (currentSongID<playlist.Length+1) {
currentSongID++;
} else {
currentSongID = playlist.Length;
}
audio.clip = playlist[currentSongID].clip;
slidePosition = slidePositionMax;
Play (); //The error is also here.
}
//Timer
if (timer > 0) {
timer -= Time.deltaTime;
}
if (fastMode && fastSource.volume < 1) {
fastSource.volume = Mathf.Min(1,fastSource.volume + Time.deltaTime * 0.25f);
audio.volume = 0.5f - fastSource.volume/2;
}
if (!fastMode && fastSource.volume > 0) {
fastSource.volume = Mathf.Max(0,fastSource.volume - Time.deltaTime * 0.5f);
audio.volume = 0.5f - fastSource.volume/2;
}
if (timer > 0) {
slidePosition = Mathf.Lerp(slidePosition,0,Time.deltaTime);
} else {
slidePosition = Mathf.Lerp(slidePosition,slidePositionMax,Time.deltaTime);
}
}
public void Pause() {
Play (playlist[currentSongID].name);
}
public void Play(string credits) {
currentSongCredits = "Now playing: " + credits;
if (FindObjectOfType<MlgMode>() != null) {//IS MLG MODE
audio.clip = mlgSong;
currentSongCredits = "Now playing: xXxSW3GST3PxXx";
FindObjectOfType<MlgMode>().StartTheShit();//Start the wubs
}
isPlaying = true;
if (!audio.mute) {
timer = 8;
}
audio.Play();
}
void OnGUI() {
if (slidePosition < slidePositionMax-0.1f) {
GUI.skin = skin;
GUIStyle style = new GUIStyle(GUI.skin.label);
style.fontSize = 16;
style.alignment = TextAnchor.MiddleRight;
Rect rect = new Rect(0,Screen.height-30+slidePosition,Screen.width,30);
//GUIX.ShadowLabel(rect,currentSongCredits,style,1);
GUILayout.BeginArea(rect);
GUILayout.FlexibleSpace (); //Push down
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace(); //Push to the right
GUILayout.Label(currentSongCredits,GUI.skin.GetStyle("SoundCredits"),GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
GUILayout.EndArea();
}
}
void ShuffleSongs() {
//Shuffle playlist using Fisher-Yates algorithm
for (int i = playlist.Length;i > 1;i--) {
int j = Random.Range(0,i);
Song tmp = playlist[j];
playlist[j] = playlist[i - 1];
playlist[i - 1] = tmp;
}
}
}
[System.Serializable]
public class Song {
public string name;
public AudioClip clip;
}
Your Play method is declared like this:
public void Play(string credits)
but you are calling it like this:
Play();
You need to include a string as a parameter when you call it.
Play("White and Nerdy - Weird Al Yankovic");
Your Play(string credits) method is expecting a string called credits. Since you call Play() without putting a string there, it is giving you an error.
What it is looking for is an overloaded method, another form of Play() without the string and when it doesn't find that you receive that error.