#💻┃code-beginner

1 messages · Page 482 of 1

eternal falconBOT
little dust
teal viper
little dust
#

its the unity version control... which I don't use

teal viper
#

It's the older version of the unity version control.

#

And even if you don't use it, it might be performing some tasks in the background

little dust
#

yeah. it should be disabled, but that could be the culprit

#

I have very few packages shared between these unity versions, and reboots and reinstalls don't help, so it pretty much... so I guess it had to be some kind of default package?

teal viper
#

What unity version are you using?

#

And did you ever used an older version with these projects(like 2019 or earlier)?

little dust
#

this new one, used an older one, but not 2019

#

2022.3.45f1 now, it was an earlier 2022.3.x one before

#

I'm doing a fresh reinstall of the editor too

#

because nothing has fixed it yet

teal viper
#

Weird. I don't think you'd have that package then.

little dust
#

yeah I have no idea

#

but it's crippling my interest in continuing, heh

#

it's brutal to have it lock up

#

like every hour or so

#

I removed the package from the manifest and I'm booting the new unity editor install now

teal viper
#

If that doesn't help,try attaching the debugger to the process and having a look at what unity does during the freeze.

little dust
#

how do I attach the debugger to the editor process?

teal viper
#

In vs debug - attach to process.

little dust
#

because the entire editor instance freezes and becomes uninteractible when this happens...

#

hm, I'm currently using Rider but I'll see

teal viper
#

Rider should have that feature too. All ides have a debugger.

little dust
#

it has that option, yep. just not sure how to access the feedback from it

teal viper
#

What feedback?

little dust
#

not sure how to 'see' what the debugger is seeing?

teal viper
#

There's gonna be a "break all" button looking like a pause button.

little dust
#

ah, it's just 'pause'. but it probably does the same thing

#

it's also failing to recognize monobehavior and other things so I'm fixing that too quick

gleaming heron
#

Trying to make a simple 2d game but the jump isn’t working

slender nymph
deft grail
gleaming heron
deft grail
#

transform.up is a Vector2 already

little dust
#

as gr4ss says. you don't need to make a new Vector2

slender nymph
gleaming heron
slender nymph
eternal falconBOT
little dust
#

you won't need "new"

deft grail
#

or the brackets

#

or velocity.x

little dust
#

rb.AddForce(transform.up * jump)

#

but you should look at the link I posted

#

or just google unity impulse force

gleaming heron
little dust
deft grail
#

oh and the main thing, !ide

eternal falconBOT
wintry quarry
#

It doesn't make sense that it would do anything at all

#

It's also unclear what you mean by the foreach resetting

gleaming heron
#

I’m confused is this

deft grail
#

but you also need to make it an Impulse force

#

rb.AddForce(transform.up * jump, ForceMode2D.Impulse)

gleaming heron
slender nymph
little dust
gleaming heron
#

Discord doesn’t work on computer for me

vagrant harness
wintry quarry
#

And destination is just a local variable inside the loop

slender nymph
# gleaming heron Discord doesn’t work on computer for me

it is also a website you can use if you cannot get the installed application working for whatever reason. it is incredibly disrespectful to those working to help you if you cannot provide proper screenshots or even follow the server's guidelines for posting code

vagrant harness
#

I mean when it's called again

wintry quarry
#

When is it called again

#

Not in the code you shared

vagrant harness
#

When the gameobjects become active, right?

wintry quarry
#

No

#

Why would that call the Coroutine

#

Start only runs once

vagrant harness
#

oh my god i just remembered on start isn't called when it becomes active im sorry

#

-_-"

wintry quarry
#

OnEnable is what you want maybe?

vagrant harness
#

Yeh

nimble apex
#

can i force a certain parameters on a component to be a constant, or cannot change for a fixed period of time (e.g size)

i spent 3 days to figure out what the hell is causing a value changing during runtime but still cant find it

wintry quarry
#

And only change things through accessor functions or properties.

From that accessor you can add logging to see what is changing it

nimble apex
wintry quarry
#

Change that particular variable

#

To not be public

#

And to use a property instead

#

And log when it changes

nimble apex
#

ok ty👍

devout onyx
#

hi guys, I am trying to configure my Unity and got this error in my VS code . The project number also doesn't show. What should I do? Thank you

devout onyx
#

there is a dotnet zip file when I download Unity

deft grail
devout onyx
#

I don't think I download it yet. Thank you

rough orbit
#

I've made a script that's supposed to make platforms fall randomly in collision with the player's collider. It works as intended as long as I disable player gravity, but when player gravity is enabled it's like the whole script is overridden and the platform falls the moment it connects with the player.

What could be the reason for this? Script:

using UnityEngine;
using System.Collections;

public class PlatformBehavior : MonoBehaviour
{
    Rigidbody myRigidbody;
    public float delay = 1f;

    void Start()
    {
        myRigidbody = GetComponent<Rigidbody>();

        myRigidbody.useGravity = false;
    }

    void Update()
    {

    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Player")
        {

            if (Random.Range(0, 3) == 0)
            {
                GetComponent<MeshRenderer>().material.color = Color.red;
                StartCoroutine(EnableGravityAfterDelay()); // Start the coroutine
            }
        else
            {
                myRigidbody.useGravity = false;
            }
        }
    }

    private IEnumerator EnableGravityAfterDelay()
    {
        yield return new WaitForSeconds(delay); // Wait for the specified delay
        myRigidbody.useGravity = true;         // Enable gravity after the delay
    }

}
astral falcon
#

Well, you say, enable gravity on the rigidbody, so it will be affected by gravity at this moment your oncollisionenter is being fired and your randomrange number is met

astral falcon
rough orbit
#

"so it will be affected by gravity at this moment your oncollisionenter is being fired and your randomrange number is met"

the issue is that it seems to not care about the random range. Every platform falls the moment player touches them, but it's only supposed to happen with 30% chance

#

And with player gravity disabled it works as intended. Just trying to understand what's going on so I can fix it

queen adder
#

pretty simple code i scrambled together to shoot a bullet on mouse click, issue being it spawns an increasing amount every time i click, fixes?

{
    if (Input.GetKeyDown(KeyCode.Mouse0))
    {
        BulletShoot();
    }
}

private void BulletShoot()
{
    Instantiate(bullet);
    rb2D.velocity = new Vector3(10, 0, 0);
    bullet.transform.position = player.transform.position;
}```
#

probably shit code im experimenting a little

deft grail
queen adder
#

yeah

#

should i put it on another object?

#

im not too sure how id get the bullets rigidbody if the script is on another object

deft grail
queen adder
#

"cannot use local variable bullet before it is declared"

#
 {
     var bullet = Instantiate(bullet);
     bullet.velocity = new Vector3(10, 0, 0);
     bullet.transform.position = player.transform.position;
 }```
#

like this?

deft grail
queen adder
#

so i have

#

a gameobject bullet and a rigidbody bullet?

#

im a little confused

deft grail
#

your bullet variable you change the type to Rigidbody2D instead of GameObject

#

then when you instantiate it you can get a reference to the object you are Instantiating, which you can change the velocity of

queen adder
#

im lost i have no idea what you mean

deft grail
#

do the 1st part first

queen adder
#

it wont let me instantiate

#

the bullet

deft grail
queen adder
#

listen uh

#

how would i instantiate something

#

that doesnt even exist in my codes eyes

#

there is no 'bullet' variable anymore

#

i changed it to myBullet and thats a rigidbody

#

where am i getting bullet from

deft grail
#
public Rigidbody2D bullet;

var spawnedBullet = Instantiate(bullet)
spawnedBullet.velocity = ...
queen adder
#

ohh the var name is different

#

im not too experienced with that bit yet my bad gang

#

still adds more bullets

deft grail
queen adder
#

i froze it notlikethis

#

i couldnt supress my urges to click and see how many shoot out

#

now it just clones the player

#

and still shoots out more

#

the issue is i need the bullets rigidbody

#

i think

deft grail
queen adder
#

no i had to give the player a rigidbody or it wouldnt work

deft grail
#

providing you actually put the Bullet into the slot

queen adder
#

its using my players rigidbody

deft grail
queen adder
#

oohh wait

deft grail
#

and you putting your player there, WILL spawn more players

queen adder
#

i didnt know you could

#

put the gameobject in the rigidbody slot

#

i figured it wouldnt work

deft grail
#

if it wouldnt work then i wouldnt tell you to do it 🤔

queen adder
#

didnt fix anything lol

deft grail
queen adder
#

full code (ignore fireRate and bulletSpeed i havent implemented those yet)

 private Rigidbody2D myBullet;

 public int fireRate;
 public int bulletSpeed;

 [SerializeField]
 private GameObject player;
 
 // Start is called before the first frame update
 void Start()
 {
     myBullet = GetComponent<Rigidbody2D>();
 }

 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Mouse0))
     {
         BulletShoot();
     }
 }

 private void BulletShoot()
 {
     var spawnedBullet = Instantiate(myBullet);
     spawnedBullet.velocity = new Vector3(10, 0, 0);
     spawnedBullet.transform.position = player.transform.position;
 }```
deft grail
queen adder
#

the player

deft grail
#

notice that one line in the Start method?

queen adder
#

ohh right,,,,,

#

im used to doing it that way oopsers

#

works now

deft grail
# queen adder im used to doing it that way oopsers

and also dont get confused at all by it being a Rigidbody2D
thats only so you can access its properties without having to use GetComponent on a GameObject.
you can Instantiate anything, GameObject or not

queen adder
#

noted

#

now to implement those other two variables atwhatcost

#

honestly shouldnt be too hard

deft grail
queen adder
#

bulletspeed done in 2 seconds

#

simple as it gets

#

i havent really messed around with time in unity before this one might be a bit tricky

deft grail
stiff fjord
#

how do i make it so i can get the predictions for the auto fill in vs code heres mye xtensiojs

deft grail
eternal falconBOT
queen adder
#

never used a courotine before so this is definitely scuffed but hows this

{
    while (true)
    {
        yield return new WaitForSeconds(fireRate);
        youCanShoot = true;
    }
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.Mouse0) && youCanShoot == true)
    {
        BulletShoot();
    }
}```
#

i already can tell that youCanShoot will probably stay true forever

#

not sure how to get it back to false

deft grail
queen adder
#

do i replace return new with wait?

marble hemlock
#

i still dont understand how i should access the scripts

deft grail
queen adder
#

oh right

marble hemlock
#

hold on incomplete thought

deft grail
queen adder
#

doesnt seem to work

stiff fjord
deft grail
stiff fjord
deft grail
queen adder
#

thats my thoughts

queen adder
stiff fjord
deft grail
#

if not then it will NEVER fire

deft grail
marble hemlock
#

found a bug yipee

queen adder
#

when i set it to true at first theres no limit to how fast i can shoot

marble hemlock
#

i just want a slot where a script can be used

deft grail
queen adder
#

heres the full code to be sure

private Rigidbody2D myBullet;

public int fireRate = 2;
public int bulletSpeed = 5;
private bool youCanShoot = true;

[SerializeField]
private GameObject player;

// Start is called before the first frame update
void Start()
{
    StartCoroutine(CountDown());
}

private IEnumerator CountDown()
{
    while (true)
    {
        youCanShoot = false;
        yield return new WaitForSeconds(fireRate);
        youCanShoot = true;
    }
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.Mouse0) && youCanShoot == true)
    {
        BulletShoot();
    }
}```
marble hemlock
#

its better to demonstrate what the thing is doing hold on

queen adder
#

gr4ss is getting hit at all sides by questions

deft grail
marble hemlock
#

no rest

#

lemme fix this bug rq i realized whats wrong

stiff fjord
deft grail
queen adder
#

the start courotine thing in start() seems to cause issues do i need it?

deft grail
#

not in start

queen adder
#

ohh right

deft grail
#
IEnumerator whatever()
{
    RUNS NOW
    yield return new WaitForSeconds(3);
    RUNS AFTER 3 SECONDS
}
#

it works like that

queen adder
#

would i put it in bulletshoot or in update,,,

deft grail
deft grail
marble hemlock
#

i need help with a completely unrelated bug :D

#

im not entirely sure whats happening though

deft grail
marble hemlock
#

i am equally confused. i shall pretend the bug doesnt exist for a bit

#

something else just broke good god
okay

#

when a player clicks on an object that has the PickUp layer, Something Is Equipped becomes true, it becomes ItemHeld, its rigid body gets messed with, and it goes to the HoldPos, which is just an empty parent object

#

When the player clicks R to drop it, it is moved from the HoldPos to the Interactable Objects parent, and it goes back to the original state it was before the player picked it up.

#

That bit is fine. Except something just broke and one of the objects disappeared. Each item has their own script. A flashlight for example would have a spot light turned on and off, or a card reader would have a script that checks if the ItemHeld matches the expected card for that object.

#

Someone suggested classes but I'm not entirely sure how I'd go about that. Nor do I entirely understand

queen adder
#

just made a reloading script, works fine except for the fact that i have to write ammo = 5 to refill the ammo again, which isnt what i want. id rather have it so ammo is back to what it was when i first defined it, is there a way to do this?

#

oh i didnt think itd embed like that oops

deft grail
queen adder
#

it all works

#

i just wanna make ammo back to what it was when i first defined it

#

makes it easier to develop later

#

instead of having to change that everytime i change the amount of ammo

deft grail
#

so you can just reference it from there

#

otherwise you would just need 2 variables in your script

#

1 for current ammo and 1 for max ammo

queen adder
#

ohh righht,,,,

#

i literally just watched that in a youtube video how could i have forgotten

marble hemlock
#

component

#

damn nvm that wouldnt work

#

the component would be on the different objects

#

i need it to check for a type of component

#

i feel like it would be classes here but idk

#

i cant just name every script the same thing

#

i mean im pretty sure i cant do that

eternal needle
# marble hemlock i mean im pretty sure i cant do that

Honestly I cant tell if you're actually asking something here. Even in the stuff above, its extremely unclear what you're actually talking about. "Something broke" couldnt be more vague as to what your actual issue is.
If you need help, you should look at the #854851968446365696 on how to ask. Show the related code and issue, and describe it concisely in a single message

marble hemlock
#

oh sorry this isnt a "something broke" thing

#

it was a "i was trying to get suggestions for a problem, and two other things randomly broke in the process of just trying to get the game to run"

#

I'm not even sure how to entirely ask the question here, because I'm not sure what its supposed to be looking for. Each item has their own activation script for their own purposes (flashlight, key card, etc.). Usually when I want to reference a script, I'd write something like private ScriptName scriptVarName; but in this case, I can't really put a specific script name, because item has its own script. Whenever a certain condition is filled, in this case, the object becoming a variable called ItemHeld, I want it to enable the script component that holds the activation script.

#

Do I kinda make more sense here?

queen adder
#

oh man trying to make a logic manager is annoying

stiff fjord
queen adder
#

for some reason after reloading for the first time, my ammo gets stuck at the max for a few seconds and i can shoot much faster than intended without needing to reload. not sure what the issue is so heres the whole code

private Rigidbody2D myBullet;

[SerializeField]
private float fireRate = 0.7f;

[SerializeField]
private int bulletSpeed = 10;

[SerializeField]
private int reloadTime = 2;

[SerializeField]
private int ammo = 5;

private bool youCanShoot = true;

[SerializeField]
private GameObject player;

[SerializeField]
private Text ammoText;

// Start is called before the first frame update
void Start()
{
   
}

private IEnumerator FireRate()
{
    youCanShoot = false;
    yield return new WaitForSeconds(fireRate);
    youCanShoot = true;
}

private IEnumerator Reload()
{
    youCanShoot = false;
    yield return new WaitForSeconds(reloadTime);
    ammo = 5;
    youCanShoot = true;
}

// Update is called once per frame
void Update()
{
    if (ammo == 0 )
    {
        StartCoroutine(Reload());
    }

    if (Input.GetKeyDown(KeyCode.Mouse0) && youCanShoot == true)
    {
        BulletShoot();
        StartCoroutine(FireRate());
        ammo -= 1;
    }

    ammoText.text = ammo.ToString();

}```
modest dust
#

You've put your OnCollisionEnter2D method within the Update method, that's one. Second, you're missing a param

pale lichen
languid spire
modest dust
#

Read what I wrote once again

#

You still have your physics methods inside of the Update method

#

They're not gonna be called from there

languid spire
modest dust
#

Now debug if the methods are called or not

#

Debug.Log("Player is now grounded.");
Debug.Log("Player is now jumping.");

In OnCollisionEnter2D and OnCollisionExit2D respectively

queen adder
#

oh you mean youCanShoot?

languid spire
#

yes

queen adder
#

so would i update the if to also include like

#

a youCanShoot2

languid spire
#

no, you would use seperate bools for each method

queen adder
#

thats what i mean

#

and then id have to add that to the if

languid spire
#

no, just use the bool that the FireRate method uses

queen adder
#

i dont think you get what i mean

#

i mean the if (Input.GetKeyDown(KeyCode.Mouse0) && youCanShoot == true)

languid spire
#

yes I do and the answer is still no

queen adder
#

so whats even the point of adding another bool if i dont reference it

#

it would still shoot while reloading

#

would it not be && youCanShoot2 == true at the end as well

languid spire
#

see what you mean, yes add it to the if, mb

queen adder
#

the ammo count keeps increasing to 5 for a few seconds after reloading

#

its 100% from this part of the code but not sure how to fix it

{
    youCanShoot2 = false;
    yield return new WaitForSeconds(reloadTime);
    ammo = 5;
    youCanShoot2 = true;
}```
languid spire
queen adder
#

starts that coroutine when ammo is at 0

#

okay wait

#

i get the issue

#

itll run multiple times

languid spire
#

ok, and how often does it start the Coroutine?

queen adder
#

how do i fix that

#

i cant just add 1 to the ammo after

languid spire
#

you have a bool you can check

queen adder
#

huh

languid spire
#
youCanShoot2 = false;
queen adder
#

oohh right

#

technically i think itd be true but yeah i get it

ancient shadow
#

decided to pick up an old project code to generate a "room" and i've realised between debugging and cleaning up that because of my previous approach to removing inaccessible areas the map can sometimes be almost solid walls

#

the map is stored as an int[,] of ones and zeros and looks like this after being built

#

here is my beautiful illustration

#

not asking for anyone to write code for me i'd honestly rather do it myself but how should i approach identifying the red regions? (they're 2x2 in this image but tbh i'd just like to find walls with a thickness of 2 or less)

floral wren
languid spire
#

instead of using 0 and 1 why not 0 == empty, 1 == edge, 2 == not edge

ancient shadow
#

wait im so dumb thats so simple

ancient shadow
languid spire
ancient shadow
#

oh snap yeah

queen adder
#

the ammo thing doesnt work

#

still

#

oh wait fixed it

languid spire
#

Add some debugging so you can see what is going on

queen adder
#

i have another issue

#

one sec i need to record it

#
private Vector3 mousePos;

// Start is called before the first frame update
void Start()
{
    mainCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();
}

// Update is called once per frame
void Update()
{
    mousePos = mainCam.ScreenToWorldPoint(Input.mousePosition);

    Vector3 rotation = mousePos - transform.position;

    float rotZ = Mathf.Atan2(rotation.y, rotation.x) * Mathf.Rad2Deg;

    transform.rotation = Quaternion.Euler(0, 0, rotZ);
}```
#

not exactly sure what causes it the tutorial doesnt have any of these problems

languid spire
#

omg

mainCam = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Camera>();

==

mainCam = Camera.main;
queen adder
#

dont blame me blame the tutorial

languid spire
#

then dont follow crap tutorials

queen adder
#

"crap tutorials" and its just the same thing but slightly longer

#

my pc isnt gonna burst into flames from the unoptimisation

ancient shadow
#

it's poor practice to learn messier code

#

it's not horrendous but it adds up over time and using the faster option from early on is the best way of getting into good the habit of using it

queen adder
#

gaah this shit is too confusing already

#

to the fiery pits of the trash bin you go unfinished slop game

languid spire
queen adder
#

no its like

#

30% my own code 70% jumbled mess of other tutorials

#

some of which i understood others i didnt

languid spire
#

well

Vector3 rotation = mousePos - transform.position;

does not return a rotation, it returns a direction which is completely different

ancient shadow
#

m[x,y] = (^pseudoRandom.Next(0,100) < randomFillPercent)? 1: 0; there's a problem implicitly converting an int to a byte on just this one specific line at the carat but i'm not super sure why it's trying to convert anything there?

#

not int[,] to byte[,] either just int to byte

#

i'm tempted to throw in casts until it works but i feel like that's not the best idea

languid spire
#

nah, cast to byte is fine. It's just C# being type safe so you are good

ancient shadow
#

what am i casting tho? the error pops up within the ()? which was fine functioning as an int comparison before

languid spire
#

you are casting int -> byte.
upcasting (byte->int) is automatic but downcasting could result in loss of data so you need to be explicit

stone pilot
#

here's the code

    private AudioSource audioSource;
    private Rigidbody2D rb;
    private bool isFalling;

    public LayerMask groundLayer;
    void Start()
    {
        audioSource = GetComponent<AudioSource>();
        rb = GetComponent<Rigidbody2D>();
        isFalling = false;
    }

    void Update()
    {
        if (rb.velocity.y < 0)
        {
            isFalling = true;
        }
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (isFalling && (groundLayer == (groundLayer | (1 << collision.gameObject.layer))))
        {
            audioSource.PlayOneShot(fallSound);
            isFalling = false;
        }
    }```
silk night
# ancient shadow the map is stored as an `int[,]` of ones and zeros and looks like this after bei...

https://www.youtube.com/watch?v=v7yyZZjF1z4&list=PLFt_AvWsXl0eZgMK_DT5_biRkWXftAOf9 this should have everything you need, its an exact match of what you are trying to do 😄

Learn how to create procedurally generated caverns/dungeons for your games using cellular automata and marching squares.

Source code:
https://github.com/SebLague/Procedural-Cave-Generation

Follow me on twitter @SebastianLague
Support my videos on Patreon: http://bit.ly/sebPatreon

▶ Play video
ancient shadow
#

I think a good chunk of the original code was appropriated from Sebastian lague (I love his voice sm) tbh

#

I’ve since desecrated it but I’ll give the video another look in a bit if I can’t get it working

graceful fractal
#

I require a bit of help please. I'm using a simple raycast to place down a structure, however, it seems that the structure is always instantiated on the position of the ground, instead of where the raycast hit.

How do I check for where the raycast hits?

I thought it would be just hit.transform but aparently it is not.

teal viper
graceful fractal
#

it works perfectly now, thank you.

#

Now I have a dynamic purchase system that allows you to click on buildings from a menu and place them in the map.

I'm so happy.

You can easily add more buildings to the menu, and it even has fancy dotween movements when opening and closing it

ancient shadow
#

nvm i just cast pretty much everything and now it works

#

the error message's position was off because it was registering the 1 and 0 as parts of the if statement ig

polar kraken
#

Hey guys,
What is the best way to limit the AI movement in this area (Same height) ?
Top-down view:

wintry quarry
#

What kind of movement and ai are we talking about?

scenic saffron
#

why is it not 2x as loud an it logs 1 both times

wintry quarry
marble hemlock
wintry quarry
#

you cannot set it to 2

#

1 is the max

scenic saffron
#

oh

marble hemlock
#

originally i was thinking of having the player raycast script check for a type of script, but maybe this can work too

#

ive been on this for like 2 days im hoping this can work

wintry quarry
#

this is a very common use case

marble hemlock
#

That second one I'm not sure what that means.

wintry quarry
#

an interface?

marble hemlock
#

Yes

wintry quarry
#

you could have an interface like:

public interface IInteractable {
  void Interact();
}```
And have your items implement that:
```cs
public class Flashlight : IInteractable {
  public void Interact() {
    ToggleOnOff();
  }
}```

Then your raycast code you use the interface to interact with it:
```cs
if (Physics.Raycast(... out hit)) {
  if (hit.TryGetComponent(out IInteractable interactable)) {
    interactable.Interact();
  }
}```
marble hemlock
#

thank you
will study

polar kraken
polar kraken
pulsar meteor
#

can anybody give me a clicker game unity turtorial with upgrades

marble hemlock
#

oh i could just have a script called item that stores all the item information, and it becomes active when the item is held, and then use that to control the scripts attached to the item

rich adder
marble hemlock
#

here i was overcomplicating it

pulsar meteor
marble hemlock
#

does anyone have any alternatives i could use for this

languid spire
#

MonoBehaviour

marble hemlock
#

🫡
it stopped imploding thank you

polar acorn
languid spire
#

please do you own research for such simple questions

marble hemlock
#

the script is going to keep changing

rich adder
polar acorn
languid spire
#

tbf MonoBehaviour is a shared parent class

polar acorn
#

Why make it easy to make a mistake and turn off a renderer or something important?

marble hemlock
#

The idea I'm trying to go for is to have the raycast search for the item script on whichever game object it just hit, and pull data from there, which is why it has serialized fields.

polar acorn
#

Whatever it is you're doing to identify the item script can also be applied to this variable

marble hemlock
#

Okay so the RaycastScript will hit an object, get the Item script component, and get an event called BeginActive, which will enable a variable called itemActivationScript, thats a serialized field, that I can attach the gameobjects actual item activation script to.

And then when I'm dropping the item, it will refer to the Item script again, and then refer to the StopActive event, which shuts down the item activation script component attached in the editor

polar acorn
marble hemlock
#

now that im saying it i feel like im just gonna get hit with a null reference error

polar acorn
#

Whether for this script or for the raycast

#

You can't just get whatever is of type Component because that's every component

marble hemlock
#

hold on let me try something

#

okay wait no I'll just say public Item itemScript;

polar acorn
#

Okay, so you do have a script Item

marble hemlock
#

yes

#

i think i might just be confusing myself im ngl

#

this should work just fine

polar acorn
polar acorn
#

Did you want to get Item or a different script every time

marble hemlock
#

i was confusing myself with my original plan and my current plan

#

OH

#

no Item isn't what's being changed.

#

the itemActivationScript is what's changing

polar acorn
marble hemlock
polar acorn
#

Why is itemActivationScript not of type Item

#

Which component are you trying to toggle on or off here

magic panther
#

I'm curious. In js, to find a thing that meets some criteria, I'd always use an arrow function. This also works fine in C#, but is it the right way to do it? How do yall think?

        RoomEntry foundRoomEntry = roomEntries.Find(roomEntry => {
            return roomEntry.roomID == roomID;
        });
marble hemlock
#

hold on i need to

polar acorn
marble hemlock
#

like this

slender nymph
marble hemlock
#

(currently using MonoBehaviour)

polar acorn
slender nymph
polar acorn
marble hemlock
polar acorn
#

Well, I'll be damned

marble hemlock
#

i mean it works disregarding that i fucked up the size when the player drops the item
but if its a ticking time bomb i have to handle that now

polar acorn
magic panther
#

Also, do yall find questions like this annoying at all? I'll be asking them a bunch more from now on since I'm taking gamedev more seriously.

polar acorn
#

Or a specific kind of script

rich adder
slender nymph
polar acorn
rich adder
#

guilty as well

stone pilot
hollow field
#

Hi all. I'm having some issues with an Editor script. I have followed a tutorial for a Field of View cone and it was working fine a few days ago but now it appears to have broken all on its own.
I have tried using breakpoints to help bug fix but it says that the breakpoint will never be reached

eternal falconBOT
rich adder
stone pilot
# rich adder check if Y velocity is above a certain treshold

I've done this but idk why it's not working

    {
        audioSource = GetComponent<AudioSource>();
        rb = GetComponent<Rigidbody2D>();
        isFalling = false;
    }

    void Update()
    {
        if (rb.velocity.y < 0)
        {
            isFalling = true;
        }
    }

    void OnCollisionEnter2D(Collision2D collision)
    {
        if (isFalling && (groundLayer == (groundLayer | (1 << collision.gameObject.layer))))
        {
            audioSource.PlayOneShot(fallSound);
            isFalling = false;
        }
    }```
rich adder
#

if(Mathf.Abs(rb.velocity.y) > sumValue) //playsound

stone pilot
#

What's wrong in the script

rich adder
#

also you probably want the collision relativeVelocity not the current rb on collision
look at the example code, is similiar usecase

languid spire
stone pilot
rich adder
#

You could but , look at the property relativeVelocity from the collision, and check if Y is above a certain value

#

Debug.Log numbers so you know what values you need with a variable

#

never hardcode a constant

hollow field
# rich adder show !code and anything else
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using static UnityEngine.GraphicsBuffer;


[CustomEditor(typeof(FieldOfView))]
public class Editor_FieldOfView : Editor
{
    private void OnSceneGUI()
    {
        FieldOfView FOV = (FieldOfView)target;
        Handles.color = Color.white;
        Handles.DrawWireArc(FOV.transform.position, Vector3.up, Vector3.forward, 360, FOV.viewRadius);

        Vector3 viewAngleA = FOV.DirFromAngle(-FOV.viewAngle / 2, false);
        Vector3 viewAngleB = FOV.DirFromAngle(FOV.viewAngle / 2, false);

        Handles.DrawLine(FOV.transform.position, FOV.transform.position + viewAngleA * FOV.viewRadius);
        Handles.DrawLine(FOV.transform.position, FOV.transform.position + viewAngleB * FOV.viewRadius);


        Handles.color = Color.red;



        foreach (Transform visibleTarget in FOV.visibleTargets)
        {
            //visibleTarget.GetComponent<Material>().SetColor("_seen", Color.red);
            visibleTarget.GetComponent<MeshRenderer>().enabled = true;
            Handles.DrawLine(FOV.transform.position, visibleTarget.position);
        }
    }
}

My FieldOfView.cs is working fine. It's adding the targets to the a list correctly

hollow field
#

i did not

#

I shall ask there

lethal elbow
#

Can anyone help me? I have a small issue of trying to get the arrow to arch down towards where it's falling. Just like a normal arrow, In my video it's always facing up

marble hemlock
#

it took me a bit to realize but all of those up there are classes

#

i just need to have it look for a specific class

#

or a subclass ig

stone pilot
rich adder
marble hemlock
#

sub class
Definitely

rich adder
#

hence why I said give it a proper minimum threshold

#

use the collision2D method I sent you

stone pilot
#

Alr

stone pilot
#

but when I drag to the left - right the sound loops

rich adder
#

you're using value of magnitude

#

of all 3 axis

stone pilot
#

I'm just new to doing these magnitude and threshold things

#

can you explain more please?

#

Like what do I've to change

#

I still don't get it

#

wait

#

I got it

#

lemme try something

#

It worked

#

Thx

queen adder
#

shit code but specifically i wanna know how to solve the issue of destroying the spawned bullet 3 seconds after its been spawned, the coroutine doesnt work so whats the next best thing?

 {
     var spawnedBullet = Instantiate(myBullet, bulletTransform.position, Quaternion.identity);
     spawnedBullet.transform.position = player.transform.position;

     StartCoroutine(FireRate());

     ammo -= 1;

     IEnumerator Decay()
     {
         yield return new WaitForSeconds(3);
         Destroy(spawnedBullet);
     }

     StartCoroutine(Decay());

 }```
rich adder
stone pilot
#

Alright man thx , But I've changed the number from 2 to 5 so when the player gets a bit high then falls the sound doesn't appear

#

now its look much better

wintry quarry
stone pilot
#

appreciate it man

queen adder
#

nothing like that but it doesnt work

rich adder
wintry quarry
queen adder
#

oh destroy has a built in timer?

#

peak

#

still doesnt work

#

oohh wait im an

#

idiot

wintry quarry
#

you're doing something weird or wrong

queen adder
#

its inside an if statement

#

bit tricky

#

spawnedBullet is only accessable within the if statement

rich adder
wintry quarry
#

there's nothing to destroy outside of it

queen adder
#

tomato tomato to what im saying

wintry quarry
#

you're creating it inside

#

I don't see what the problem is.

queen adder
#

me neither but its definitely affecting it

wintry quarry
#

You would need to show your code

#

and show what's happening

queen adder
#

yeah i am hold on

#
        if (Input.GetKeyDown(KeyCode.Mouse0) && youCanShoot == true && youCanShoot2 == true)
        {
            var spawnedBullet = Instantiate(myBullet, bulletTransform.position, Quaternion.identity);
            spawnedBullet.transform.position = player.transform.position;

            ammo -= 1;
            StartCoroutine(FireRate());

        }```
wintry quarry
#

You can literally just do this:

 if (Input.GetKeyDown(KeyCode.Mouse0) && youCanShoot == true && youCanShoot2 == true)
 {
     var spawnedBullet = Instantiate(myBullet, bulletTransform.position, Quaternion.identity);
     Destroy(spawnedBullet, 3);```
queen adder
#

didnt work last time

wintry quarry
#

It works

#

You probably have multiple places where you're spawning bullets or something

#
&& youCanShoot == true && youCanShoot2 == true)```
This is a huge red flag to me
queen adder
#

i have absolutely no idea how else id write that to be honest

wintry quarry
#

so if this isn't working, it's because different code is actually spawning the bullet

queen adder
#

do you wanna see my jumbled mess of this script

wintry quarry
#

yes

#

share the full script

wintry quarry
queen adder
#
    private Rigidbody2D myBullet;

    [SerializeField]
    private float fireRate;

    [SerializeField]
    private float reloadTime;

    [SerializeField]
    private int ammo;

    [SerializeField]
    private int maxAmmo;

    private bool youCanShoot = true;
    private bool youCanShoot2 = true;

    [SerializeField]
    private GameObject player;

    [SerializeField]
    private Text ammoText;

    public Transform bulletTransform;
    
    // Start is called before the first frame update
    void Start()
    {
        fireRate = GameObject.Find("Base Stats").GetComponent<Logic>().fireRate;
        reloadTime = GameObject.Find("Base Stats").GetComponent<Logic>().reloadTime;
        ammo = GameObject.Find("Base Stats").GetComponent<Logic>().ammo;
        maxAmmo = GameObject.Find("Base Stats").GetComponent<Logic>().maxAmmo;
    }

    private IEnumerator FireRate()
    {
        youCanShoot = false;
        yield return new WaitForSeconds(fireRate);
        youCanShoot = true;
    }

    private IEnumerator Reload()
    {
        youCanShoot2 = false;
        yield return new WaitForSeconds(reloadTime);
        ammo = maxAmmo;
        youCanShoot2 = true;
    }

    

    // Update is called once per frame
    void Update()
    {
        if (ammo == 0 && youCanShoot == false)
        {
            StartCoroutine(Reload());
        }

      

        if (Input.GetKeyDown(KeyCode.Mouse0) && youCanShoot == true && youCanShoot2 == true)
        {
            var spawnedBullet = Instantiate(myBullet, bulletTransform.position, Quaternion.identity);
            spawnedBullet.transform.position = player.transform.position;

            ammo -= 1;
            StartCoroutine(FireRate());

            Destroy(spawnedBullet, 3);

        }

        ammoText.text = ammo.ToString();


    }

    

}
wintry quarry
#

But the bigger problem is those variable names are not descriptive

queen adder
#

works for me

#

i know exactly what they mean within my headspace

wintry quarry
#

Do you have any errors in the console window?

queen adder
#

no

wintry quarry
#

Then there's nothing that would cause this not to wrok

queen adder
#

do you not think its something like

wintry quarry
#

oh

#

I see the problem

queen adder
#

only runs on click but it needs 3 seconds

#

whats the issue

wintry quarry
#

you're only destroying the Rigidbody

#

var is hiding the problem

#
Destroy(spawnedBullet.gameObject, 3);``` is what you need
queen adder
#

aah okay

wintry quarry
#

Because it's really

Rigidbody spawnedBullet = Instantiate(myBullet, bulletTransform.position, Quaternion.identity);```
#

without the bigger context of knowing the prefab is a Rigidbody it wasn't clear that myBullet was not a GameObject

queen adder
#

works perfect now

#

also yeah i told you this is a little scuffed

#

actually i have one other super small issue

#

sometimes if i click super fast i end up shooting 6 bullets instead of 5

#

right after it reloads if i click it wont register having used a bullet

stone pilot
#

I was wondering if I can make everytime the player fall from a higher place the volume be higher and when he fall from a small distance the volume be low how can I achive this?

#
    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Ground"))
        {
            if (collision.relativeVelocity.y > 4)
            {
                audioSource.PlayOneShot(fallSound);
            }
        }
    }```
queen adder
#

i wouldnt know how to implement it but id assuuume youd make the velocity gain over time

rich adder
queen adder
#

ooh actual volume

#

oops

#

i dont know what i was thinking

languid spire
#

use Lerp where min=0, max is max volume and t = % of max distance

rich adder
wintry quarry
rich adder
#

why does reload control youCanShoot2 instead of canShoot 🤔

stone pilot
#

I've done this and it actually worked

    {
        if (collision.gameObject.layer == LayerMask.NameToLayer("Ground"))
        {
            float fallSpeed = Mathf.Abs(collision.relativeVelocity.y); // Get fall speed

            if (fallSpeed > minFallVelocity)
            {
                float volume = Mathf.Clamp((fallSpeed - minFallVelocity) / (maxFallVelocity - minFallVelocity), 0f, 1f) * maxVolume;
                audioSource.PlayOneShot(fallSound, volume);
            }
        }
    }```
queen adder
#

probably is

rich adder
#

why are there two?

queen adder
#

they made me add it

#

originally reload and firerate both used the og

rich adder
#

idk who "they" is , also you should know why you did something

queen adder
#

because like

languid spire
queen adder
#

i forgot how to word it

#

she but yeah

rich adder
#

idk just doesnt make sense to me
if reload reset canShoot2 but the if statement checks

youCanShoot == false)
        {
            StartCoroutine(Reload());
queen adder
#

oh wait

#

turned it into 2 lets see

wintry quarry
#

See this is why you should name them something more descriptive like isReloading and isCoolingDown

#

now you're getting confused yourself.

queen adder
#

aaaaaah minor issue

#

pretty sure its fixed now

#

but fine

#

if i were to make another gun would i just

#

copy paste this entire file and add like

#

ammo += 3 or something

#

is that how other people do it ive never tried this sorta thing before

amber spruce
#

hey so for my game i will have multiple different walls and they each will need a different amount of velocity to break through them should i store those values in smth like a scriptable object or just a script that is attached to the wall objects

rich adder
#

you make them variables that you can change with the same script

#

don't use number constants like that

queen adder
#

im already using a logic file

#

but i dont really get what to

#

do with it

#

ive imported its variables into this script

#

but then what

rich adder
#

I only have a script called Weapon and literally has 7-8 different weapon types just by adjusting some sliders

#

or using SOs

queen adder
#

im completely lost

#

id assume itd be something like

#

checks

rich adder
swift elbow
rich adder
#

hence why you think you need multiple of the same script cloned but with slightly diff numbers internally

queen adder
#

yeah but how do i not do that

rich adder
#

use variables lol

amber spruce
rich adder
#

instead ammo += 1

queen adder
#

am i not already

rich adder
#

ammo += amountToRefill

#

etc

queen adder
#

that was an example

#

because im not sure how to like

#

implement the logic script properly

#

i dont know how to seperate different weapons in the same script

swift elbow
swift elbow
rich adder
swift elbow
#

my message was assuming they were just a model and a collider

rich adder
#

you change the im in the inspector you start getting different behaviors @queen adder

amber spruce
queen adder
#

how do i actually implement the shotgun part

#

what would seperate them

#

id assume its something like

queen adder
#

youd get the objectname of what youre holding

#

and if it matches these are yhour stats

rich adder
#

give each shot/bullet a slightly different rotation, Put your shoot logic inside a for loop that has a bulletsPerShot
or for rays, what I do is give my original ray offsets in each direction, or Random.insideUnitSphere inside the loop

amber spruce
swift elbow
#

that way you dont have to worry abt edge cases

queen adder
#

i mean how do i actually likeAUURRGH this is hard to word

#

what do i put in the script to discern the difference between weapons

#

so it knows what youre holding

#

and then it does this

rich adder
queen adder
#

never heard of either of those in my life sadok

rich adder
#

public enum Weapons { Shotgun, Pistol, AssaultRifle }

#
  [SerializeField] Weapons weaponType;
 public Weapons GetCurrentWeapon() {return weaponType;}
 // or
 public Weapons CurrentWeapon => weaponType;```
queen adder
#

yeah i

#

dont get it hold on

#

ive got the hang of it

#

one issue

amber spruce
#

so in my OnCollisionEnter2d function i want it to knock the thing colliding with it back so basically apply knockback how can i achieve this

queen adder
#

not sure how to like

#

give them all the default

#

yk what i mean

#

like i can make it so the ammo for one is larger

#

but not sure how they all actually spawn bullets

rich adder
amber spruce
#

so it gets knocked back the way it came

rich adder
queen adder
#

ignore that

#

im asking how i make it so all the guns still spawn bullets

#

like they actually use the rest of the script

rich adder
#

all it changes usually is the prefab of the bullet they spawn

queen adder
#

hold on i havent finished it yet

verbal dome
queen adder
#

this is way too confusing for me just give me a minute rq

verbal dome
queen adder
#

im asking questions as im doing it

#

do you want me to be sat in silence completely lost on what to do

#

the guide im reading says to do it like this but i get an error, is it outdated or am i being stupid

{
    case Weapons.ARRIFLE:```
verbal dome
#

Wheres the rest of it?

queen adder
#
{
    Pistol,
    ARRifle
}

private void GunLogicThing()
{
    switch (weapon)
    {
        case Weapons.ARRIFLE:

    }
}
rich adder
queen adder
#

its just a red line under case Weapons.ARRIFLE:

#

says

verbal dome
#

Read it carefully

rich adder
#

btw R in AR is Rifle 😛 (Assault Rifle Rifle)

queen adder
#

control cannot fall out of switch from final case label

#

yeah well ar sounds too small

amber spruce
rich adder
#

Vector2.Reflect

queen adder
#

oh its just one of those things where it stays red until you like add a seperate line

#

oopsers

rich adder
queen adder
#

its missing a break

rich adder
#

there is no break

queen adder
#

yeah

#

for what i put inbetween would it be like

#

maxammo += 10

#

or what

#

you seemed against that

#

but not sure what else id do to increase it

rich adder
#

because hardcoding numbers is bad practice

queen adder
#

yeah but how else would i increase it without actually adding numbers

#

maxammo + maxammo,,?

rich adder
#

maxAmmo += ammoIncreaseValue

#

literally the whole point of variables

queen adder
#

sounds a little out there for just adding more but fine

#

i dont think adding a 3 would break the whole script

amber spruce
rich adder
#

coding numbers you cant change without recompiling the code is just silly

queen adder
#

if anything would this not be even more time consuming

verbal dome
#

Lol what?

rich adder
amber spruce
queen adder
#

yeah i get it

#

mumble mumble

rich adder
rich adder
queen adder
#

i cant help being a little slow you are in the beginner channel

rich adder
#

I get it but this is an important concept about any language

#

you shouldnt move onto something this complex without these bases of knowledge

#

imagine a script that had over 300 lines and you hardcode the waitTime to 3 seconds..What if you later on want 2 seconds, now you have to go through 300 lines to find each time waitTime Changes for something just to change it to 2..thats the time consuming

queen adder
#

i understand

#

does make it feel a little more professional which feels nice

rich adder
#

without variables coding would be a messy nightmare

thorny basalt
#

What you do is you store it to a const variables

rich adder
#

not a good idea if you want a modular weapon in this case 😉

#

every weapon will have the same const

#

but yeah if you need a constant value def use const types. I use them for my strings Animation name to HashID

thorny basalt
#

Correct. But if it was hardcoded before making it const would be ideal.

rich adder
#

yup exactly. Variables are goat

#

const string c_HorizontalAxis = "Horizontal"
Input.GetAxis(c_HorizontalAxis );

amber spruce
queen adder
#

&& is labelled as an invallid expression term after i changed the getkeydown to getmousebutton, fixes?
if (Input.GetMouseButton(0)) && cooldown && reloading)

rich adder
queen adder
#

im trying to make it so the code works if im holding m1

#

which i assume getmousebutton does

#

from what ive been told

amber spruce
rich adder
#

indeed

amber spruce
#

i was trying collision.GetContant(0).collider.normal lol

languid spire
amber spruce
# rich adder indeed
collision.rigidbody.AddForce(Vector2.Reflect(collision.relativeVelocity, collision.GetContact(0).normal));

so this should be good

rich adder
queen adder
#

worked with getkeydown

rich adder
languid spire
amber spruce
queen adder
#

i am looking-

amber spruce
#

ok

languid spire
queen adder
#

oh oopsie

#

but thats an error everybody makes

rich adder
#

is your IDE configured?

queen adder
#

telling me to just look doesnt help im blind as hell

queen adder
languid spire
rich adder
#

look with your mind not your eyeballs

queen adder
#

okay anyway finally set up the ar

#

since the pistol is the same as regular would i just

#

break immediately

#

or do i not need the pistol and thats what default is for?

rich adder
#

you dont need the switch inside the Weapons cript

#

unless ur doing something very specific..

queen adder
#

so what do i uh

#

do then

rich adder
#

All guns have bullets, all guns have reload time, all guns have shoot rate

#

etc

#

all that changes are the values 🤷‍♂️

queen adder
#

youre confusing me

rich adder
#

how so ?

queen adder
#

i dont know what to keep and what to change sob emoji

rich adder
#

why are you putting a switch in the weapon

queen adder
#

i showed you this code earlier and you said nothing then

#

im just following this guide

rich adder
#

the switch goes somewhere else, for example if you want to evaluate a pickup

#

or maybe what type of weapon you're switching too

queen adder
#

so i

#

dont need a switch

#

effectively

rich adder
#

the values are what determine behavior unless the weapon is wildly different then you would make an child class/abstract

queen adder
#

it says i need a switch

rich adder
#

what is "it"

queen adder
#
{
    
    case Weapons.ARRifle:
        maxAmmo += (ammoChanger * 5);
        ammo += (ammoChanger * 5);
        fireRate -= (fireRateChanger * 2);
        break;
rich adder
#

no doing this in the weapon itself is horrid

#

see you aint listen.. lol

queen adder
#

i can hardly understand half of what youre saying

#

i was just following the guide sob

rich adder
#

but you're hardcoding numbers again

queen adder
#

?? where

rich adder
#

defeats the whole point of variable if ur gonna use a giant switch to use magic numbers

rich adder
queen adder
#

ive never even used a switch before now

rich adder
#

those are "magic"

queen adder
#

dude do you want me to just

#

type out

rich adder
#

I never said to use a switch though ?

queen adder
#

ammochanger + ammochanger + ammochanger + ammochanger + ammochanger

#

this did

#

this looked concice to me

rich adder
#

i said use ENUM if you want to just "label" which gun is which

#

i never said start using a switch

queen adder
#

i googled a guide for enum and this is what i got

#

i assumed it just came with it

#

i told you i dont know what an enum is

rich adder
#

so you're doing some other thing I never said to do and said I'm the one confusing you lol

queen adder
#

difference between not saying something and telling me not to do something

#

i made an assumption based on the guide and your info

#

and it was wrong

#

no biggie

rich adder
#

well you outta make grown up decisions of your own I cant be responsible lol

#

I tried to show you example of enum you said "I think I got it "

queen adder
#

its a new thing im trying i cant be expected to be a master of the craft

#

im gonna make mistakes and im gonna make assumptions

rich adder
#

yes because you dont really need it so far, but you asked so I answered enum lol

#

I said if you want diff weapons use the same script, just change values

languid spire
queen adder
#

you said the way to do that was with enum

rich adder
#

an enum discerns the different weapon type, you just dont need a switch to do anything inside the script itself. It can be used for OTHER scripts to determine what Weapon.cs you have

queen adder
#

gaah this is too much

#

just show me what i was meant to do

#

and ill do that

rich adder
#

you wont learn anything copy n paste

timber tide
#

enum can work and a match, but this is assuming you have all the data in that statement that you need

queen adder
#

its not copy and paste

rich adder
#

jumping into different weapon mechanics and you dont know what enum is, its your fault for going to deep so quickly

timber tide
#

usually want to have that data in an object to make things more readable

queen adder
#

i have that

#

logic object

rich adder
#

what does "logic object" mean. everything in c# is an object

queen adder
queen adder
#

and i refer to that in the other script

rich adder
#

empty what?

queen adder
#

just an empty

#

empty gameobject

rich adder
#

so a gameobject

#

how does it hold "gun stats"

#

if you already have gun stats why are you hardcoding the numbers

#

just make different versions of gunstats

lapis frigate
#

is there a problem with using random integers in unity?

queen adder
#

it just has like

queen adder
#

the fireSpeed and reload timer and stuff

rich adder
lapis frigate
#

so like its Random rnd = new Random(); just like normal c#?

queen adder
#

to SPECIFICALLY keep it consistent

rich adder
#

Random.Range

#

etc.

deft grail
queen adder
#

i can change stuff in the logic object and itll change for the gun

lapis frigate
#

like if I put 1-100 it can be 1 -100?

rich adder
lapis frigate
#

oh ok

#

tnx

#

so like I gotta put 1 -101 right?

rich adder
#

correct

lapis frigate
#

aight tnx

rich adder
#

only float version is max inclusive

#

float value = Random.Range(0f, 10f) // 0-10f

queen adder
#

oh wait i got sidetracked

#

i still need to know how to switch weapon types

rich adder
#

just make each weapon a different GO

deft grail
rich adder
#

then toggle it with an arrays index

queen adder
queen adder
rich adder
deft grail
queen adder
#

thats the

#

same thing

#

dont copy the same script but use the same script

rich adder
#

yea they misunderstood what i said, they originally wanted to clone the same script and change the name for diff weapons

deft grail
#

use 1 script on 5 GameObjects
dont make 5 Scripts for 5 GameObject

queen adder
#

right

#

but what im asking is

#

how would i change the variables for each one

#

without changing them for the others

deft grail
rich adder
#

in the inspector?

timber tide
#

for the most part, if you derive classes from a gun script, you just can compare those object types instead of an enum

rich adder
#

thats why I said to make them variables lol

deft grail
#

the values arent shared between scripts, unless they are a static

queen adder
#

gaah it just seems alot more scuffed

#

also im not

#

actually using gun models

deft grail
lapis frigate
#

I'm a little confused

timber tide
#

if you want to use a single gun class then you'd want an enum to specify these "types"

rich adder
#

very common

deft grail
#

you need to select a specific one

rich adder
deft grail
# queen adder also im not

well if you want you can make just 1 GameObject, just change the "gun" by selecting a different ScriptableObject for the stats

queen adder
#

i have no idea what that is hold on

lapis frigate
#

tnx

rich adder
#

You can also make it a struct but yes SOs are always easier

deft grail
rich adder
#

since they live on the disk

deft grail
#

and then you create instances of this in the assets folder, and change the variables

queen adder
deft grail
timber tide
#
public enum GunType
{
  Pistol,
  SMG,
}

[CreateAssetMenu]
public class MyClass : ScriptableObject
{
  public GunType GunType;
  public int FireRate;
  ...
}```
#

adding onto that

rich adder
#

if you use SOs you technically dont even need enum

#

the instance is the weapon type

queen adder
#

just give me a step by step hold on

#

ive never even heard of a scriptableobject before now

#

this is alot of information and my brain isnt braining it

deft grail
queen adder
#

it is for me

timber tide
#

it's just a way to express data instead of hardcoding everything

rich adder
#

they are like regular pocos except they have extra unity functionalities

deft grail
#

its literally a basic C# class

timber tide
#

assuming you want more than 1 pistol or 1 smg

queen adder
#

listen just

#

so i make a new script

deft grail
deft grail
#

correct

rich adder
#

ScriptableObjects / MonoBehaviours always belong in new file. Never in the same file

deft grail
# queen adder and do this

the only important things are ScriptableObject and [CreateAssetMenu] that make it different from your normal MonoBehaviour scripts

#

the rest is exactly the same (in terms of basic variables and stuff)

queen adder
#

i have no idea what to put in this script

#

what do i actually put in here

deft grail
#

whatever your gun needs

#

sounds, sprites

queen adder
#

so is it essentially the same as my logic script

queen adder
#

so is that useless

deft grail
#

the point is you hold the DATA for your guns elsewhere

deft grail
queen adder
#

then why am i

#

redoing it here

deft grail
#

you can change your gun from a pistol to a raygun to a sniper in 0.2 seconds

#

with 1 click

queen adder
#

thatll make this useless

#

because i cant like

rich adder
#

you're creating an obects that holds different datas for each wep

queen adder
#

hold on

deft grail
#

create the script

queen adder
#
reloadTime = GameObject.Find("Base Stats").GetComponent<Logic>().reloadTime;
ammo = GameObject.Find("Base Stats").GetComponent<Logic>().ammo;
maxAmmo = GameObject.Find("Base Stats").GetComponent<Logic>().maxAmmo;
ammoChanger = GameObject.Find("Base Stats").GetComponent<Logic>().ammoChanger;
fireRateChanger = GameObject.Find("Base Stats").GetComponent<Logic>().fireRateChanger;```
queen adder
#

this is what i have in my bullet firing script

timber tide
#

yikes

queen adder
#

people in coding beginner when you code like a beginner

deft grail
#

as simple as that

rich adder
#

no memes allowed 😉

deft grail
#

to get your data

timber tide
#

Find is a newb trap don't worry everyone falls for it

queen adder
#

i have no idea what you mean by this

deft grail
#

so you can reference the DATA

queen adder
#

uh just

deft grail
#

but first make the actual ScriptableObject class

queen adder
#

i have no idea what that is-

deft grail
#

once you actually do this all, you will understand why its so useful and good

rich adder
deft grail
#

youve done this a hundred times

queen adder
#

it isnt processing in my head

#

what do i write instead of that

#

the script name?

rich adder
deft grail
#

first

#

leave your gun script

#

we dont care about that right now

#

MAKE a NEW script

queen adder
#

yeah

deft grail
#

name it GunSO for example

queen adder
#

i just named it SO

rich adder
#

call it WeaponData imo

#

more clear

deft grail
#

yeah for example

queen adder
#

later just go on

willow scroll
#

The scriptable objects should usually be named with "Data" at the end, since they hold data

deft grail
queen adder
#

yeah

deft grail
#

make it inherit from ScriptableObject instead of MonoBehaviour

queen adder
#

done that

deft grail
#

now just add your variables

queen adder
#

same as the logic script

deft grail
#

public int FireRate; for example

queen adder
#

done

rich adder