If and ActiveSelf. Only works once. With example project - c#

First time developer here and I am struggling with a problem.
I made a mock up of the problem in regular Unity 3d since the VR scares some talented people away from helping me. That can be downloaded here: https://gofile.io/d/qHDlUZ
File is a zip called: SoundTestTroubleShooting. A Unity build on 24.7MB
Follow the number on the buttons.
Button 1 "Choose Music" Activates the Cube.
Button 2/4 "Play Music" Checks if cube is activated and plays song if that is the case.
Button 3 "Restart Scene" Stops the music and reloads the scene.
Cube has dontdestroyonload.
After that i press button 2/4"PlayMusic" and it cant find that the cube is active.
WHY?! Going nuts here.
Old text with the entire problem here:
In my game I have four songs in the start menu that my player gets to choose from. He chooses one by pressing one of four buttons. This activates an empty GameObject, lets call it SmileMusicTrigger. SmileMusicTrigger has a dontdestroyonload and is deactivated by the other three buttons if player wants to switch song.
Player press Begin which moves him into position. He presses start and this button has:
public GameObject SmileMusicTrigger;
public GameObject EpicMusicTrigger;
public GameObject GodMusicTrigger;
public GameObject VikingMusicTrigger;
void OnTriggerEnter()
{
if (SmileMusicTrigger.activeSelf)
{
AudioManager.instance.Play("Smile");
}
if (EpicMusicTrigger.activeSelf)
{
AudioManager.instance.Play("Epic");
}
if (GodMusicTrigger.activeSelf)
{
AudioManager.instance.Play("God");
}
if (VikingMusicTrigger.activeSelf)
{
AudioManager.instance.Play("Viking");
and my game start and and the music begins fine.
When player dies he restarts the scene with restart button which has this to say about the music:
void OnTriggerEnter()
{
AudioManager.instance.Stop("Smile");
AudioManager.instance.Stop("Viking");
AudioManager.instance.Stop("God");
AudioManager.instance.Stop("Epic");
}
}
after this code has run the scene restarts and it does not seem like my Start button can check the active state of SmileMusicTrigger again. I know SmileMusicTrigger is active through the restart of the scene because I see it under dontdestroy in the hierarchy when game is running and it is checked as on in inspector. But I cant seem to call it again. Thus my music only plays once.
Audiomanager is also under Donotdestroy so I dont think the problem is there.
Sorry if this is messy. I am working really hard to get this working but I am so stuck right now.
Thankful for any help.
All the best!

Related

Sound not played when collecting Coin

I am new to game development and building Endless Runner game, where Rocky(Player) can collect coins, and I applied cool sound effect when coin is being collected by player but that is not played. Here is the detail what I did so far,
First I create an Empty Object named it CollectCoin and drop coin collect sound on it.
Inspector View of CoinCollect, as can see CoinSFX is applied on AudioClip property
Coin Object look like this at inspector view and my AudioSource applied there in scripts. Also check Is Trigger checkbox
Player Inspector View
And finally my Class
public class CoinCollect : MonoBehaviour
{
public AudioSource coinFX;
public void OnTriggerEnter()
{
coinFX.Play();
this.gameObject.SetActive(false);
}
}
What and where i done wrong so far? I am stuck here since yesterday. Please help me.
There was nothing wrong with my script, and I dragged both Coin Collect Sound and Background Music at correct place, problem was with my Unity, I don't know when I muted "Mute Audio" option in unity, or its muted by default i have no idea but my problem is resolved. That is why my sound was not playing

My sound keeps repeating in Unity whenever I press the left mouse button instead of playing once

I am working on an FPS game in Unity using C#. This code is supposed to play a sound once whenever you press the left mouse button, but whenever I press it the sound plays repeatedly with no break between the sounds. I did find a question very similar to this one but I couldn't implement the answer into my code, Link: https://forum.unity.com/threads/how-to-play-a-sound-with-c.206152/. Any help will be appreciated.
using UnityEngine;
public class GunFire : MonoBehaviour
{
void Update()
{
if (Input.GetButton("Fire1"))
{
AudioSource sound = gameObject.GetComponent<AudioSource>();
sound.Play();
}
}
}
Every time you click the button you want the sound to play. Then the problem is that you are using GetButton which returns true every frame that button is pressed.
Try using GetButtonDown which will only return true the first frame the button is pressed regardless of whether or not you are holding it.
Links to the API's pages for those functions : GetButton, GetButtonDown
You may also need to check if you have looping on or off on the audio source.
Check your AudioSource for loop checkmark, it must be false
If you need loop true on this AudioSource, you can use PlayOneShoot() instead of Play().
p.s. Execute GetComponent() every time you need play the sound is a bad practice. It will be better to cashe it once.

Networked AR Unity can't make a bullet a child of the ground plane stage

I am trying to make an AR game and am currently using Unity's high-level networking classes. I have set my player prefab to be spawned in one of the two network spawn locations, which are both childs of the Ground Plane Stage. When user has tapped to make their ground plane stage and has tapped to be the host, their player character appears. Unfortunately, if they press the fire button, the bullets appear above the stage and unscaled, meaning they aren't parented to the stage. This confuses me because I've checked many times and the bullet emitter is a child of the player, and in my code it references said emitter. Thus I'm rather confused why the bullets don't seem parented.
I've attempted to attach a script to make the bullet emitter a child of the player when it spawns. I've also tried making it a child of the stage when it spawns. I've tried making the player character not dependent on the Network manager spawning it when the player joins, but then that leads to other networking problems when it comes to controlling the character, but it can shoot then.
The only one that was relatively successful was making the bullet a child of the stage when it spawned, but it would only stay in one place. Attempting to make the bullet a child of the player did nothing
//This is the class I'm trying to use to make the object a child of
something
public class AddToBeetle : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
GameObject player =
GameObject.FindGameObjectWithTag("Player");
transform.SetParent(beetle.transform, false);
}
}
It rarely prints any error messages. I hope that I can eventually get the bullet to spawn in front of the player model when the button is pressed.
Oh gosh I was dumb, the one thing I didn't apply the script to was the player's bullet emitter. It was super late and that's probably why I missed trying it on that. Anyway I hope this helps someone else if they ever have to deal with AR and Unity.

Press a in game Button to play a animation

I recently thought about adding a InGame Button to my Game. It's not a GUI or UI Button, it's a Block, added to a Wall for example.
Dummy Code:
OnTriggerEnter(c:Collider) {
if(c.gameObject.tag =="Player")
{
//Text = "E to interact!"
if(key.pressed("e")
{
//Connect the Button to a specific Block, play a Animation
}
}
}
So how do I connect a specific Block to the Button, and if I press e, Play the Animation just on the specific block? Please keep in mind that I'm new in Unity.
Thanks for helping out!
I don't know if you already managed to create your animation, so I-ll explain from scratch. When in the editor, select your Door and press ctrl+6 to open the animation window. From here you can animate your block. When you will be done creating the animation, your block object will have a new script attached to it: an animator. You can see the animator state machine in the animator window
These are two different things:
Animation: Defines a single animation (translation, rotation, change of color, ...)
Animator: defines when an animation occurs for the corresponding gameobject. An animator can have variables (for example, a bool) that define which is the next animation to be played
Any object can have an animator (your button can have one to move when it is pressed. You door can have another one to open / close)
For instance, in your button animator, you should have three states: Idle, Press, UnPress.
The state Press will contain the animation "press" with speed 1. The state UnPress will contain the animation "press with speed -1
Then, still in the animator window, You will create links between Idle and the two other states and add a trigger condition called "OnPress" (for example)
You can do the same to animate your door
In your Button code, you will then write
public Animator Door; // In the editor, give a reference to your door. It must have an Animator script for this to work
OnTriggerEnter(c:Collider) {
if(c.gameObject.tag =="Player")
{
//Text = "E to interact!"
if(key.pressed("e")
{
GetComponent<Animator>().SetTrigger("OnPress"); // The button's animator goes to "pressed" state
Door.SetTrigger("Open"); // The door's animator goes to "open" state
}
}
}
Then you could add another trigger to unpress the button
One more thing: When you say "Connect the button to the block", I feel like you misunderstood something: Your button script should be already added to the block in the editor
Look at these two links for more information on animations:
http://docs.unity3d.com/Manual/animeditor-UsingAnimationEditor.html
http://docs.unity3d.com/Manual/AnimatorWindow.html

Scene is not loading properly

I have 2 scenes in my game. 1st for menu and 2nd for Game itself. There is one button on screen called "Play". This button will let player load Game Scene. And if user press Escape from Game Scene, it will load Main Menu. I've uploaded code for it below,
if (Input.GetKeyDown (KeyCode.Escape)) {
SceneManager.LoadScene ("Menu");
}
The above code(Written in Update()) exits the Game Scene, But The player remains on screen. And so if I again press Play, the Game Scene does not display player. I've searched for it much, but I can't find any understandable reason. Can anyone help me please.
The problem could be that you have called DontDestroyOnLoad(player) somewhere.
Have you tried making a different scene and loading that one to test if that also happens?

Categories