Search Unity

Gun Sound, Following tutorial Youtube old code<help>

Discussion in 'Scripting' started by Lightme, May 23, 2018.

  1. Lightme

    Lightme

    Joined:
    Jul 15, 2017
    Posts:
    23
    Hello,

    My problem is as follow, I was going trough an old tutorial whereby the script is JavaScript but Unity doesn't support that anymore only C#. So I hope some genius can help me out with this code:

    Code (JavaScript):
    1. function Update () {
    2.     if(Input.GetButtonDown("Fire1")) {
    3.         var gunsound : AudioSource = GetComponent.<AudioSource>();
    4.         gunsound.Play();
    5.         GetComponent.<Animation>().Play("GunShot");
    6.     }
    7. }
    To:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class MYCLASSNAME : MonoBehaviour {
    5.  
    6. void  Update (){
    7.     if(Input.GetButtonDown("Fire1")) {
    8.         AudioSource gunsound = GetComponent.<AudioSource>();
    9.         gunsound.Play();
    10.         GetComponent.<Animation>().Play("GunShot");
    11.     }
    12. }
    13.  
    14. }
    But I get all kind of erros.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I would suggest that you change your class name, so it's maybe: "MyClassName" (casing of the text.. what you call it is up to you*).
    Rememeber to change the file name and save if you change the name. The filename and script name must match.

    Next, Try changing:
    Code (csharp):
    1. GetComponent<AudioSource>(); // no '.' before '<'
    After that, if you have errors, please add the error message and line #(s) to your post.

    Also, assuming it works, you should cache your components in Awake().
    Code (csharp):
    1. AudioSource gunsound;
    2.  
    3. void Awake() {
    4.    gunsound = GetComponent<AudioSource>();
    5.  }
    Like that, for both components.

    Of course, make sure there is an audio source and animation component on the game object.
    You may want to use the animator, instead.. but that could be another topic. :)
     
    Lightme likes this.
  3. Lightme

    Lightme

    Joined:
    Jul 15, 2017
    Posts:
    23
    First off all thank you, I did changed the code as you mentioned:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GunFire : MonoBehaviour {
    5.  
    6.     void  Update (){
    7.         if(Input.GetButtonDown("Fire1")) {
    8.             AudioSource gunsound = GetComponent<AudioSource>();
    9.             gunsound.Play();
    10.             GetComponent.<Animation>().Play("GunShot");
    11.         }
    12.     }
    13.  
    14. }    
    And these are the errors I am getting back:

    Assets/Scripts/GunFire.cs(10,16): error CS1001: Unexpected symbol `<', expecting identifier
    Assets/Scripts/GunFire.cs(10,26): error CS1001: Unexpected symbol `>', expecting identifier
    Assets/Scripts/GunFire.cs(10,28): error CS1001: Unexpected symbol `)', expecting identifier

    It seems it have to do with this:

    Code (CSharp):
    1.             GetComponent.<Animation>().Play("GunShot");
    The Animation is there and also the GunShot sound is there as well...
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yes, okay you updated one line of code but not the other. :) I wrote one example, but thought / meant to use it for both.
    change that to:
    Code (csharp):
    1. GetComponent<Animation>().Play("GunShot");
     
    Lightme likes this.
  5. Lightme

    Lightme

    Joined:
    Jul 15, 2017
    Posts:
    23
    Aaaah... sorry (n00b at work... wanna try out an idea ... )
    Thanks, yeah kind of logic of course...

    But it worked all of it is now working fine and perfect...
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, I'm glad it's working. Take it easy. :)

    For the future, probably try to find C# tutorials, when at all possible. Usually they are there.

    Plus, if all of this is new, I think you'd really appreciate and learn a lot of useful stuff from the Unity tutorials in the Learn section of this site.
    Here: https://unity3d.com/learn
    or here: https://unity3d.com/learn/beginner-tutorials

    Some good stuff! Enjoy Unity.
     
  7. Lightme

    Lightme

    Joined:
    Jul 15, 2017
    Posts:
    23
    Yeah I know...

    Followed a few of them... and the other thing is for the idea I want to do I want too use this particulair tutorial as a basic for my game. Will be the first 'real' game I will build.

    Anyhow are there any tutorials about the converting from JavaScript to C#?
     
  8. winterfluxstudio

    winterfluxstudio

    Joined:
    May 4, 2018
    Posts:
    61
    Wait, so you're aware of Unity tutorials.... but just don't want to use them or...? You would rather attempt to write javascript and convert it to C# than just learn C# in the first place?

    https://unity3d.com/learn/tutorials/lets-try/shooting-with-raycasts
     
    Lightme likes this.
  9. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I think learning from C# tutorials would be easier.
    If you ever couldn't find a tutorial to help you and couldn't write the script yourself, there is this: https://github.com/Unity-Technologies/unityscript2csharp

    If you google "UnityScript to C# converter" you could find others.

    That being said, for the most part, if you understand C# code, viewing the UnityScript equivalent shouldn't be too hard to convert on your own, it would just take longer. In small examples, I doubt it would make much difference.
     
    Lightme likes this.
  10. Lightme

    Lightme

    Joined:
    Jul 15, 2017
    Posts:
    23
    Now I followed like 4 of them, also Space Shooter who have a lot of old code and constantly need to search the whole forum.

    Kind of gave up on that one.

    But I wanted to follow a particulair tutorial that wasn't listed there for other kind of games. This one contained old code. But I kind of figured it out already. The creator has placed under the video the C# script thank God.

    Anyway I am not being ignorant, I already started like 5 video's and didn't wanted too gave up directly on this one. Besides I think fixing code makes you also learn a lof of coding.

    Error buggin, but my main business is making websites with fixing errors and solving them or asking others who know beter I learned a lot from that practice.

    You should try sometimes ;) (no seriously!)
     
  11. winterfluxstudio

    winterfluxstudio

    Joined:
    May 4, 2018
    Posts:
    61
    Didn't mean to come across as suggesting you were ignorant - just curious if there was a specific reason. I do agree that there is a lot of the tutorial stuff thats old. The documentation is the best source for snippets of code in my opinion.

    I stopped doing websites almost a year ago (backend Ruby on Rails) after about 7 years of doing it. just got tired of it and had been using Unity/CryEngine/Unreal for the last 4/5 years.

    if you want to make large games (or stuff with lots of different mechanics) i find it's kind of a scavenge hunt through the documentation but most of it is in there (even some really complicated stuff like open world building, high level/low level networking etc)

    It would be nice if they made some more indepth videos. A lot of them are superficial. The "Tower Defence" tutorial is a key example. 90% of the "behind the scenes code" is already there. the video is "click this dropdown menu and select this item. drag and drop this onto that, next, click this other dropdown and choose...."

    it's practically useless. only discusses setting up the game using the "drag and drop" system. You could argue "just look through the code" but they should really have done it "from scratch" or not at all.
     
  12. piggypiggyrun

    piggypiggyrun

    Joined:
    Jan 28, 2020
    Posts:
    1
    Is
    How To Make An FPS - Unity Tutorials - Part 004 - Animation
    ?
     
  13. Lightme

    Lightme

    Joined:
    Jul 15, 2017
    Posts:
    23