Hi all,
Totally new to Unity and c# – having a ton of fun but also some frustration. I’ve learned a lot already through various tutorials and am currently working on a simple project. It’s a game where the player (a rolling sphere) moves over various triggers, and each trigger plays a piece of music (and stops playing the previous piece of music). Kind of like an interactive music playlist where the triggers are the ‘play next song’ button.
I’m having a hell of a time figuring out how to write this script and what I’m supposed to attach it to. I’ve searched through the forums, but a lot of the posts that may have answered this question contain references to old versions of the software or lots of code I don’t yet understand how to read. Any guidance would be hugely appreciated!
Thanks! I’m going through this now, unfortunately running into some obsolete API usages.
Any help would be appreciated…
The script the tutorial wants me to have created by 11:55 in the video follows at the end of this post. The console gives me several errors when I write it:
-
Some scripts have compilation errors which may prevent obsolete API usages to get updated. Obsolete API updating will continue automatically after these errors get fixed.
-
Assets/Scripts/ThrowObject.cs(20,79): error CS0103: The name `newVector3’ does not exist in the current context
-
Assets/Scripts/ThrowObject.cs(20,61): error CS1502: The best overloaded method match for `UnityEngine.Rigidbody.AddRelativeForce(UnityEngine.Vector3)’ has some invalid arguments
-
Assets/Scripts/ThrowObject.cs(20,61): error CS1503: Argument #1' cannot convert
object’ expression to type `UnityEngine.Vector3’
-
Some scripts have compilation errors which may prevent obsolete API usages to get updated. Obsolete API updating will continue automatically after these errors get fixed.
usingUnityEngine;
usingSystem.Collections;
publicclassThrowObject : MonoBehaviour {
publicGameObjectprojectile;
privatefloatthrowSpeed = 2000;
//Usethisforinitialization
voidStart () {
}
//Updateiscalledonceperframe
voidUpdate () {
if (Input.GetButtonDown(“Fire1”))
{
GameObjectthrowThis = Instantiate (projectile, transform.position, transform.rotation) asGameObject;
throwThis.GetComponent().AddRelativeForce (newVector3(0,0,throwSpeed));
}
}
}
I didn’t test it, but this is what I would do:
// this follows the naming convention "MusicObject00", "MusicObject01", ...
// for the objects this script is attached to
public GameObject AudioPlayer; // empty that has an audiosource component
public AudioClip[] Songs;
void OnTriggerEnter (Collider other) {
if (other.gameObject.tag == "Player") { // tag the rolling sphere as Player
int ThisNumber = -1;
if (this.gameObject.name.Substring(11,2).Parse >= 10) {
ThisNumber = this.gameObject.name.Substring(11,2).Parse;
}
else {ThisNumber = this.gameObject.name.Substring(12,1).Parse;}
AudioPlayer.GetComponent<Audiosource>().Stop();
AudioPlayer.GetComponent<Audiosource>().clip = Songs[ThisNumber];
AudioPlayer.GetComponent<Audiosource>().Play();
}
}