Audio won't play on instantiated object in Unity - audio

I have a Canvas prefab called gameOverScreen which has an audio clip attached to it. It is cloned whenever the following function is called.
public static void GameOver()
{
GameObject gameOverScreen = Instantiate(Resources.Load("gameOverScreen")) as GameObject;
gameOverScreen.audio.Play();
}
The problem is the audio doesn't play. I worked around it by using audio.PlayOneShot() in another function, but is there some reason why it doesn't work in this case?

In c# you can use this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class SoundController : MonoBehaviour {
public AudioClip clip;
void Start () {
AudioSource.PlayClipAtPoint(clip, Vector3.zero, 1.0f);
}
}
and in Javascript you can use this:
var myClip : AudioClip;
function Start () {
AudioSource.PlayClipAtPoint(myClip, transform.position);
}
But if you want play sound in instantiate:
#pragma strict
var prefabBullet : Transform;
var forwardForce = 1000;
var myClip : AudioClip;
function Update()
{
if (Input.GetButtonDown("Fire2"))
{
var instanceBullet = Instantiate (prefabBullet, transform.position,
Quaternion.identity);
instanceBullet.GetComponent.<Rigidbody>().AddForce(transform.forward *
forwardForce);
AudioSource.PlayClipAtPoint(myClip, transform.position);
}
}

Related

How to get hand position on Hololens?

I have this script in order to get hand position with hololens, but it does not work and it gives me this error:
Errore CS0123 Nessun overload per 'GetPosition' corrisponde al delegato 'Action'
Can someone help me ?
This is the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.WSA.Input;
public class Hand : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
void Awake()
{
InteractionManager.InteractionSourcePressed += GetPosition; // No overload for 'GetPosition' corresponds to delegate 'Action<InteractionSourcePressedEventArgs>'
}
private void GetPosition(InteractionSourceState state)
{
Vector3 pos;
if (state.sourcePose.TryGetPosition(out pos))
{
Debug.Log(pos);
}
}
}
The error code pointed out that if you want to register for the InteractionSourcePressed input event, you should modify the callback function GetPosition to match the delegate of the InteractionSourcePressed event.
So, in this case, you can refer to the following code to fix this issue:
private void GetPosition(InteractionSourceState state)
=>
private void GetPosition(InteractionSourcePressedEventArgs args)
More information please see:Windows-specific APIs (XR.WSA.Input)

NullReferenceException Fire Ball

I'm working on this game breakout-game
And i'm trying to make the ball fire and collide with the wall bouncing, to do that I did what he said, I added a script and put this code:
public class BallMove : MonoBehaviour {
private Rigidbody rb;
public float ballVelocity = 800f;
private bool isMove;
// Use this for initialization
void awake() {
rb = GetComponent<Rigidbody> ();
}
// Update is called once per frame
void Update () {
if (Input.GetButtonDown ("Fire1") && isMove == false) {
transform.parent = null;
isMove = true;
rb.isKinematic = false;
rb.AddForce (new Vector3(ballVelocity,ballVelocity,0));
}
}
}
I understand every line of this code, but when I try to play this, I got a nullReferenceException ,I can run the game but when I press the key I'm getting a error,someone know why? and what happens?
You should use Awake(), not awake(). In your case, you are using a "customized" function, and not the "official" one used by the Unity engine.
So, the engine cannot start that function by itself, and rb stills null when used in the Update().
Example:
void Awake() {
rb = GetComponent<Rigidbody> ();
}

error : is a field but used as a type

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication5
{
public class ClientContext
{
private string p;
public ClientContext(string p)
{
// TODO: Complete member initialization
this.p = p;
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//First construct client context, the object which will be responsible for
//communication with SharePoint:
private ClientContext context = new ClientContext("#url");
//then get a hold of the list item you want to download, for example
public List list;
public ClientContext
{
list = context.Web.Lists.GetByTitle("001_CFR_DPV_COST_REV_SHARING");
}
//note that data has not been loaded yet. In order to load the data
//you need to tell SharePoint client what you want to download:
context.Load(result, items=>items.Include(
item => item["Title"],
item => item["FileRef"]
));
//now you get the data
context.ExecuteQuery();
//here you have list items, but not their content (files). To download file
//you'll have to do something like this:
var item = items.First();
//get the URL of the file you want:
var fileRef = item["FileRef"];
//get the file contents:
FileInformation fileInfo = File.OpenBinaryDirect(context, fileRef.ToString());
using (var memory = new MemoryStream())
{
byte[] buffer = new byte[1024 * 64];
int nread = 0;
while ((nread = fileInfo.Stream.Read(buffer, 0, buffer.Length)) > 0)
{
memory.Write(buffer, 0, nread);
}
memory.Seek(0, SeekOrigin.Begin);
// ... here you have the contents of your file in memory,
// do whatever you want
}
}
}
this is the complete code.
I don't know why it is showing error. I searched for the error "is a field but used as a type" and I tried that but it didn't help. Please help with a solution Code to this since I am new to this. Thank you in advance.
What are you trying to achieve by this lines of code?
public partial class Form1 : Form
{
...
public ClientContext
{
list = context.Web.Lists.GetByTitle("001_CFR_DPV_COST_REV_SHARING");
}
}
What is public ClientContext {} inside class Form1 ?
It seems that you intended to create constructor to a class in another class and for compiler it looks more like a property but without accessors (get, set) as if it is a Type or smth like this.
Try to put get; set; accessors inside if you intended to create property:
public List Context
{
get
{
list = context.Web.Lists.GetByTitle("001_CFR_DPV_COST_REV_SHARING");
return list;
}
}
Or change it to method instead :
public void GetClientContext()
{
list = context.Web.Lists.GetByTitle("001_CFR_DPV_COST_REV_SHARING");
}

Unity3D - How to make a texture changing mute button/toggle?

I'm trying to make a mute button for in my android game menu, so when I press the button, the texture changes from a playing speaker symbol to a muted speaker symbol (which I've already made in Photoshop).
So when the audio is playing it will have a 'speaker' symbol, but when I press it, it will change to a 'muted speaker' symbol (a speaker with a cross).
Thanks in advance, any help is appreciated!
first we make a maintexture as the texture that is always used and on awake we put our texture1(speaker) assign to it and if button is pressed we change it to texture2(mute)
public Texture2D Texture1;
public Texutre2D Texture2;
public bool textureBool;
void Awake() {
textureBool=true;
void OnGUI(){
if( GUI.Button( rect , textureBool ? texture1:texture2 ) )
{
textureBool = !textureBool;
}
}
You can also use UI button in this way:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ChangeSprite : MonoBehaviour {
public Image image;
public bool isPress = false;
public Button button;
public Sprite Fsprite;
public Sprite Ssprite;
// Use this for initialization
void Start () {
image = button.GetComponent <Image>();
}
// Update is called once per frame
public void ChangeSprites () {
isPress = !isPress;
if ( isPress == true )
{
image.sprite = Ssprite;
}
else
{
image.sprite = Fsprite;
}
}
}

MvvmCross binding iOS Gestures

I'm searching a way how can i bind ios gesture like UILongPressGestureRecognizer to ICommand or MvxCommand in MvvmCross, thanks.
PS : I found an example here but i can't figure out how to do that.
From the example you found and from the current MVVM Cross source I did the following
public static class MvxBehaviourExtensions
{
public static MvxLongPressGestureRecognizerBehaviour LongPress(this UIView view)
{
var toReturn = new MvxLongPressGestureRecognizerBehaviour(view);
return toReturn;
}
}
and
public class MvxLongPressGestureRecognizerBehaviour
: MvxGestureRecognizerBehavior<UILongPressGestureRecognizer>
{
protected override void HandleGesture(UILongPressGestureRecognizer gesture)
{
// Long press recognizer fires continuously. This will ensure we fire
// the command only once. Fire as soon as gesture is recognized as
// a long press.
if (gesture.State == UIGestureRecognizerState.Began)
{
FireCommand();
}
}
public MvxLongPressGestureRecognizerBehaviour(UIView target)
{
var lp = new UILongPressGestureRecognizer(HandleGesture);
AddGestureRecognizer(target, lp);
}
}
and to bind
set.Bind(this.LongPress()).For(lp => lp.Command).To(c => c.DoTheStuffCommand);

Resources