#I made a script for an SMG a few months ago but I forgot how it works.

1 messages · Page 1 of 1 (latest)

limber shore
#

using UnityEngine;

public class SMG : MonoBehaviour
{
public float damage = 10f;
public float Range = 80f;
//public float recoil = 30f
public float SMGfireRate = 45f;

private float nextTimeToFire = 0f;

public AudioSource SMGAudioSource;
[SerializeField] private AudioClip[] SMGfireSFX;

public Camera fpsCam;

// Update is called once per frame
void Update()
{


    if (Input.GetButtonDown("Fire1") && Time.time >= nextTimeToFire)
    {
        nextTimeToFire = Time.time + 1f / SMGfireRate;
        //recoil = transform.position(0f, 0f, 0f);
        Shoot();


    }
}
void Shoot()
{
    RaycastHit hit;
    if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, Range))
    {
        Debug.Log(hit.transform.name);

        Target target = hit.transform.GetComponent<Target>();
        if (target != null)
        {
            target.TakeDamage(damage);
        }
    }

    SMGAudioSource.PlayOneShot(SMGfireSFX[Random.Range(0, SMGfireSFX.Length)]);
    SMGAudioSource.PlayOneShot(SMGfireSFX[Random.Range(100, SMGfireSFX.Length)]);

}

}

#

What am I supposed to do?

compact apex
#

Ask an actual question. You say you've forgotten how it works, that's not a question.
You then ask what you are supposed to do, but that's impossible for us to know, you haven't said what you want to do, what you've tried, if you get an error etc. etc.

#

Also

tawdry hillBOT
#

Use codeblocks to send code in a message!

To make a codeblock, surround your code with ```
To use C# syntax highlighting add cs after the three back ticks.

For example:
```cs
Console.WriteLine("Hello World");
```

Produces:

Console.WriteLine("Hello World");

To send lengthy code, paste it into https://paste.myst.rs/ and send the link of the paste into chat.

limber shore
compact apex
#

Now, cool trick, double click or something on the error and it'll bring you to the line with the error

compact apex
#

Sounds like you're trying to access a value in an array at an index that doesn't exist

compact apex
#

How long is your SMGfireSFX array?

limber shore
compact apex
#

Then what use is the second playoneshot, with the error?

limber shore
#

You mean this?

compact apex
#

You don't have to constantly reply btw.
But yes, what use does that second line have, if the index goes to at most 100?
All of that is done through the first line, any index past 100 won't exist and give an error

limber shore
#

Should I remove the second line?

compact apex
#

I don't know why it's there so I can't tell you, but if you don't have more than 100 sound effects, it's going to throw an error if you try to access one at an index past 100

compact apex
#

Again, I can't tell you what to do, because I have no idea what you're trying to do. Remove the second line, test it, is that what you want? Yes no? I don't know what you're trying to do.

limber shore
#

I don't know what I'm doing wrong

#

Everytime I launch the game and press the fire button, the game crashes

#

And gives me an error on line 2

#

Of 46

compact apex
#

The error you're getting, is as I said, trying to access an array at an index that doesn't exist

#

Have you tried removing that line, what happens?

limber shore