- Home /
C#: Changing current position of an object in y.
Hi I'm new to Unity and I was managing to disappear objects making sounds when collided. I found scripts where the object gets destroyed but only when the sound ends, so what I want is to immediately disappear I thought to translate it to a new position in y where you can no longer see it but can still hear the sound.
Sorry if my code is a mess but I'm still learning and the problem is that what I have here does what I want except that position x and z get reset instead of staying in their current position. It would be great if someone could help me out, thanks a lot!
 using UnityEngine;
 using System.Collections;
 
 public class DisplayScore : MonoBehaviour {
 
     public string text;
     public bool display = false;
     public AudioClip sound;
     float move = 10;
     
     void OnTriggerEnter(Collider iCollide)
     {
         if(iCollide.transform.name == "First Person Controller"){
             display = true;
             GameVariables.score+=100;
             audio.Play();
             transform.position = new Vector3(transform.position.x,
transform.position.y - move);
             Destroy(collider);
             Destroy (gameObject, 4);    
         }
     }
     void OnTriggerExit(Collider uCollide)
     {
         if(uCollide.transform.name == "First Person Controller"){
             display = false; 
         }
     }    
     void OnGUI(){
         if(display == true){
             string text = GameVariables.score.ToString();
             GUI.Box (new Rect(400,50,100,30), text);                
         }
     }
 }
Answer by jenci1990 · Dec 02, 2014 at 08:18 PM
Before destroy, play it with this:
 AudioSource.PlayClipAtPoint(sound, transform.position);
Now you can destroy, without Stop the sound.
Thank you this was the way I was looking for at first.
Your answer
 
 
             Follow this Question
Related Questions
Remove object based on its position alone 1 Answer
Object won't instantiate at parents position(solved) 2 Answers
destroy object on collision 1 Answer
particle effect activation 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                