- Home /
Question by
kumarrohith24081999 · Jul 19, 2020 at 06:58 AM ·
unity 2dvisual studio
ArgumentNullException: Value cannot be null. (Trying to add audio to my 2d game but struck with this error)
CODE:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class player : MonoBehaviour
{
// Start is called before the first frame update
public AudioSource AS;
private Rigidbody2D R;
public static int t = 0;
private Vector3 scale;
private Vector3 size;
private Vector3 coin;
void Start()
{
scale = new Vector3(1.0f, 1.0f, 1.0f);
size = new Vector3(0.5f, 0.5f, 0.5f);
coin = new Vector3(Random.Range(0.0f, 1.0f), 0.0f, 0.0f);
R = GetComponent<Rigidbody2D>();
AS = gameObject.GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKey(KeyCode.A))
{
R.AddForce(-transform.right * 0.5f, ForceMode2D.Impulse);
}
if (Input.GetKey(KeyCode.D))
{
R.AddForce(transform.right * 0.5f, ForceMode2D.Impulse);
}
if (Gold.hit == 2 && transform.localScale != size)
{
transform.localScale = transform.localScale - scale;
Gold.hit = 0;
}
else
{
print("enter A or D");
}
if (t ==1)
{
AS.Play();
t = 0;
}
}
void OnCollisionEnter2D(Collision2D col)
{
if(col.gameObject.name=="A"|| col.gameObject.name == "B")
{
transform.localScale = transform.localScale + scale;
}
}
}
Error I am getting:
screenshot-2020-07-19-at-122020-pm.png
(54.1 kB)
Comment
did you make sure that a valid audio clip is assigned to the audiosource?
Answer by N-8-D-e-v · Jul 19, 2020 at 06:30 PM
Assign an audio clip to the audio source in the inspector