r/Unity3D – Telegram
r/Unity3D
265 subscribers
12.7K photos
15.6K videos
14 files
48.1K links
News about the Unity engine and project showcases from Reddit. Made possible with @reddit2telegram (@r_channels).
Download Telegram
Need help creating an inventory system in Unity2D (with provided noscript)

Hi everyone,

I'm currently developing a 2D game in Unity and need some advice on creating an inventory system. I've already set up some noscripts to manage interactions like gathering and chopping trees, but I'm a beginner at noscripting and am not entirely sure if I'm on the right track.

My current noscripts: Woodcutting.cs: This noscript handles tree chopping, item creation (e.g., logs) through prefabs, adding experience to the player, and tree respawning.

PickupItem.cs: A noscript attached to collectible objects to handle interactions and retrieve item data.

ItemData.cs: A ScriptableObject I use to define tools. Here is the complete noscript:

csharpCopier le codeusing UnityEngine;

CreateAssetMenu(fileName = "New Item", menuName = "Inventory/Item")
public class ItemData : ScriptableObject
{
public string itemName; // Name of the item
public Sprite itemIcon; // Item icon
public string toolType; // Type of the tool, e.g., "Axe", "Pickaxe", etc.
public int toolPower; // Power of the tool (useful for woodcutting)

// New variables added
public bool isStackable; // Indicates if the item can be stacked
public int maxStackSize; // Maximum stack size, e.g., 10, 20, etc.
}


My goal: I want to create an inventory where collected items (like logs) appear and can be managed. Currently, my ItemData is mainly used for tools, but I'd like to know if I should structure it differently to manage resources or other types of items.

My questions:

1. Is my ItemData.cs well-structured to be used as a base for a general inventory?
2. Is there a better way to manage different types of items in the inventory?
3. Any advice on how to handle item display, stacking, and interaction in the inventory?

Any advice, suggestions, or code examples would be greatly appreciated! I'm new to development, so feel free to explain even the basic concepts.

Thanks in advance for your help!

https://preview.redd.it/m07z7i6hnryd1.png?width=1920&format=png&auto=webp&s=2d651a131b17245d4e6febd0bca5b49a5009c0dc

https://preview.redd.it/9xd95j6hnryd1.png?width=1920&format=png&auto=webp&s=0f4934beebe94d9c5635747abeaedfe025181ca1



https://redd.it/1gizoe8
@r_Unity3D
What was your "roadmap" that you followed to learn the Unity Engine? What did you do (what Videos, what courses, what habits etc. did you follow) to become an Intermediate/Advanced coder?

Im currently in this very narrow space where i just cant find alot of ways to escape being a beginner coder. So im interested to hear what you guys did to become better. And perhaps what examples there are that helped you speed up your journey when it comes to OOP.

https://redd.it/1giwia8
@r_Unity3D
Project is invalid

Hello everyone, first time coming into the sub, I encounter an issue with a specific file and hope this sub can help me understand the issue, any advise is welcome and thanks.

so I made a game test file, it had a map with a few prefab like player, bullet, health pack and enemy patrol.
and when I tried to open it on other app or try cloning it and opening on a different file, an error pop up "Project file invalid". I tried changing the name of some of the file, but no change.
And to clarify my original file can be reopen and played. the issue is I can try to open it in a new computer or in a new file project

https://redd.it/1gj40sa
@r_Unity3D
This media is not supported in your browser
VIEW IN TELEGRAM
Today I finished the Procedural animation in Unity tutorial series. Hope the Unity community enjoys it! (Link in denoscription)

https://redd.it/1gj6duv
@r_Unity3D
How do i lock my camera?

So I've made this platformer game where the camera uses a code to follow the character smoothly, but no i have this problem that when my character moves to the walls the camera shows the void , how do i make it so the camera only follows the character upwards(Y axis)

https://redd.it/1gj6oze
@r_Unity3D
Building a new enemy AI but wondering if I’m misusing ray casting
https://redd.it/1gj7rjr
@r_Unity3D
Problem in Pillars Generation for Flappy Bird

https://preview.redd.it/zpnrc2nsduyd1.png?width=153&format=png&auto=webp&s=a1356b9cc33c4b86baf99c3e89502cda83d9b1b1

I am at the start of my Gamedev Journey and am trying to recreate Flappy bird and for some reason, the pillars are generating like this.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class MovingPillar : MonoBehaviour
{
    SerializeField Vector3 startPos;
    SerializeField float endPos;
    SerializeField float waitTime = 2f;
    SerializeField GameObject pillar;
    SerializeField float pillarVelocity = 1f;
    Rigidbody2D pillarRB;
    bool Death = false;
    void Awake()
    {
        pillarRB = GetComponent<Rigidbody2D>();
    }

    void Start()
    {  
        pillarRB.velocity = new Vector2(-pillarVelocity, 0);
        StartCoroutine(GeneratePillars());
    }
   
    void Update()
    {
        if (transform.position.x <= endPos)
        {
            Destroy(pillar);
        }
    }

    IEnumerator GeneratePillars()
{
    while (Death == false)
    {
        yield return new WaitForSecondsRealtime(waitTime);
        GameObject pillarClone = Instantiate(pillar, startPos, Quaternion.identity);
        Rigidbody2D pillarRBClone = pillarClone.GetComponent<Rigidbody2D>();
        pillarRBClone.velocity = new Vector2(-pillarVelocity, 0);

        // Wait for the pillar to reach the end position and then destroy it
        float distanceToTravel = Mathf.Abs(endPos - startPos.x);
        float travelTime = distanceToTravel / pillarVelocity;
        yield return new WaitForSecondsRealtime(travelTime);
        Destroy(pillarClone);
    }
}


>This is the code and I am unable to fix the issue. Would appreciate some suggestions.

https://redd.it/1gj9c7y
@r_Unity3D
How we cut down our Domain Reload from 25s -> 6s

We, like probably most of you, also hate waiting for noscript compilation & domain reloading. Making a minor change and waiting for that long sucks. So we (mostly my colleague) looked into what we had to do to make it better. Note that this worked for us, YMMV.

###Buy a better CPU
Throwing money at the problem solves some of it. We upgraded one of our office PCs to a 12700K and cut off a decent chunk for that PC (iirc it cut down the time from like 32s to 25s for him).

###Assembly Definitions
The official Unity response to this problem is mostly "use assembly definitions". And you probably should do that where applicable. Most (all?) of your plugins probably already use them. Other than that we only use 3: Some editor noscripts, our tests and everything else. We probably could've done that better but I'm not gonna spend a month rewriting half our codebase in the hopes to shave off a second or 2.

###Domain Reload

The core of this info comes from 2 articles:

1. https://johnaustin.io/articles/2020/domain-reloads-in-unity

2. https://blog.s-schoener.com/2023-08-16-why-your-unity-project-is-slow/

And here's the profilers we used:

1. https://openupm.com/packages/com.needle.compilation-visualizer/

2. https://openupm.com/packages/com.unity.editoriterationprofiler/

3. https://github.com/pschraut/UnityHeapExplorer

4. https://github.com/Unity-Technologies/ProjectAuditor

I recommend reading both articles, though the 2nd article helped me most. Make sure you go through the profilers and actually look at the data before you start tinkering. So what actually worked for us?

We got rid of most serializable data. Yep, that's about it. We have quite a few lists that we generate on startup and marking them as NonSerialized was like 95% of our improvements. We also marked (almost) everything that was shown in the inspector as such and got rid of a bunch of Serializable attributes on classes that didn't need it.

We tend to only use the inspector for debugging purposes anyway so that worked for us. Even marking public & private variables/properties that were not part of a MonoBehaviour as NonSerialized showed improvements, minor as they were.

###HotReload
Yeah it comes up often and I've had mixed results. It only works like half the time for me (or less?) but that's still time saved when it does work. There's a list on his site on what it works for (here: https://hotreload.net/faq), if you're curious.

If anyone has any other tips on this, would love to hear em!

https://redd.it/1gjcp9l
@r_Unity3D
This media is not supported in your browser
VIEW IN TELEGRAM
Seeing other’s space games inspired me to share my project.

https://redd.it/1gjd9ve
@r_Unity3D
🔥1
This media is not supported in your browser
VIEW IN TELEGRAM
Boss Snake! Climb high and strike when the giant snake's head is within reach!
https://redd.it/1gjj1r0
@r_Unity3D
This media is not supported in your browser
VIEW IN TELEGRAM
We Created a Dynamic Gesture Recognition Tool for Unity – Perfect for Interactive Applications!

https://redd.it/1gjcl19
@r_Unity3D
This media is not supported in your browser
VIEW IN TELEGRAM
We're working on our survival game. Where would you guess our game is set? Which country and region? Virtual high five to those who find the right answer!

https://redd.it/1gjk0ok
@r_Unity3D