Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ScavengerH · Aug 31, 2016 at 06:44 PM · error messagestuckbegginer

Simple error but I'm just a begginer and cant figure this out, due to outdated API.

Hello helper, and thanks for helping me, so my problem is my playing sound effects for fire sound, are too low for enemies and players, when i run the game and i try to shoot using my left click mouse the fire sound is really low and i'm trying to control its volume by a script, same for the enemies fire sound so here is my code below, and error message i get.

Error Message : error CS0120: An object reference is required to access non-static member `UnityEngine.AudioSource.volume'

using UnityEngine; using System.Collections;

public class PlayerController : MonoBehaviour { //A variable(box in inspector where you can adjust the speed of the moving character(Player)) public float speed = 5.0f; public float padding = 1; public GameObject bulletShot; private float xmax = 78; private float xmin = -79; public float projectileSpeed = 10; public float projectileRepeatRate = 0.2f; public float health = 350; public AudioClip fireSound;

 void Start() {

 Camera camera = Camera.main;
 float distance = transform.position.z - camera.transform.position.z;
 xmin = camera.ViewportToWorldPoint(new Vector3(0,0,distance)).x + padding;
 xmax = camera.ViewportToWorldPoint(new Vector3(1,1,distance)).x + padding;
 }


 void Fire() {

 Vector3 offSet = new Vector3 (-2,0.40f,0);
 GameObject beam = (GameObject)Instantiate(bulletShot, transform.position + offSet, Quaternion.identity);
 beam.GetComponent<Rigidbody2D>().velocity = new Vector3 (-projectileSpeed,0,0);
 fireSound = AudioSource.volume(2,3);
 AudioSource.PlayClipAtPoint(fireSound, transform.position);
 }




 // Update is called once per frame
 void Update () {
 if (Input.GetMouseButtonDown(0)) {

 InvokeRepeating("Fire", 0.0001f, projectileRepeatRate);

 }
 if (Input.GetMouseButtonUp(0)) {

 CancelInvoke("Fire");

 }
 //Allowing the player to move in the X-axis
 if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) {
 transform.position = new Vector3 (
 Mathf.Clamp(transform.position.x - speed * Time.deltaTime, xmin,xmax),
 transform.position.y,
 transform.position.z
 );

 } 
  if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) {
 // Rotate the object around its local X axis at 1 degree per second
 transform.position = new Vector3 (

 Mathf.Clamp(transform.position.x + speed* Time.deltaTime, xmin, xmax),
 transform.position.y,
 transform.position.z
 );

 }


 }

 void OnTriggerEnter2D (Collider2D collider){

 Projectile missile = collider.gameObject.GetComponent<Projectile>();

 if (missile) {
 health -= missile.GetDamage();
 missile.Hit();
 if (health <= 0 ) {


 Destroy(gameObject);
 }

 }
 }
 }
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hexagonius · Aug 31, 2016 at 07:45 PM 0
Share

Please format the code so one can read it properly

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Jessespike · Aug 31, 2016 at 07:59 PM

AudioSource needs a reference, for lines like this

 fireSound = AudioSource.volume(2,3);


Try something like:

 AudioSource audioSource = GetComponent<AudioSource>();    // store reference
 fireSound = audioSource.volume;

Manual - AudioSource

Scripting API - AudioSource

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

67 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why do i get an error ? please help i've stuck for 20 min 1 Answer

getting the error CS0201 and i cant fix it. 2 Answers

hello! Im a begginer trying to solve this. OverflowException: Number overflow. CreateGame..ctor () (at Assets/scripts/CreateGame.cs:28) 0 Answers

Help me please i am stuck. 1 Answer

Why the Unity is not opening? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges