How to make a potion of strength

Hello everyone,

I’m developing a game on Unity and I need help implementing a force potion script. Here’s what I’m trying to do:

  1. Strength Potion: A potion that, when touched by the player, adds a random amount of strength to their character.
  2. Variability: The strength gained should be random, but within specific limits (for example, between 0 and 100).
  3. Respawn: After being hit, the potion should disappear for a short time before reappearing at a new location.
  4. Console Log: Optionally, display in the Unity console the strength gained each time a player touches the potion.

I already wrote a script, but when the player steps on the potion, nothing happens. Here is the current script:

using UnityEngine;

public class PotionDeForce: MonoBehaviour
{
    public int forceGagneeMin = 0;
    public int forceGainedMax = 100;

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            // Generate a random number between forceGagneeMin and forceGagneeMax inclusive.
            int forceGagnee = Random.Range(forceGagneeMin, forceGagneeMax + 1);

            // Add the gained strength to the player (you need to implement player management).
            // For example, if the player has a "Player" class with an "AddForce" method, you could do something like:
            // other.GetComponent<Player>().AddStrength(strengthGained);

            // Reappearance of the potion after a certain delay.
            Invoke("ReappearancePotion", 2f);

            // Optionally, you can display the strength gained in the console.
            Debug.Log("Strength gained: " + forceGained);
        }
    }

    void ReappearancePotion()
    {
        // Code to move the potion to a new position or reset its original position.
        // You should implement this according to your game design.
    }
}

I welcome any advice or corrections that could improve this script. Thank you in advance for your help !

Adapt it as needed and be sure to include specific details about your current implementation for more accurate answers. But be careful the script will be put in an object!
Sorry the script is in french and english pls make the script in english or french
I’am french

Hello,

Not to be offensive, it seems that you have little knowledge about Unity and c# so I recommend starting by a concrete tutorial, you may find plenty available on youtube, I highly recommand freeCodeCamp.org for this.

If you’d like a quick explanation for why chat gpt’s solution didn’t work, here’s what you needed to do to achieve your goal :

  • Set a public attribute (eg. strenght) to the script that controls the player. Let’s say you named it Player.
  • After the line int forceGagnee = ... you need to update this attribute by doing other.GetComponent<Player>().strenght += forceGagnee;. This will look at the gameobject which holds the collider of the potion. Note that for this to work both the player and the position need a Collider component, one of which needs a Rigidbody and the player needs to have its tag set to “Player”.
    This will work but the potion will stay in place. To make it disappear and spawn a new one, you’ll need to create a script whose purpose is to spawn the potions. This scripts should have a method Spawn that spawns a given potion prefab. From there you’ll have to :
  • Call Spawn on Start of the potion spawner (you’ll have to put an object with this component on your scene).
  • Destroy potions upon collision with the player by adding the line Destroy(gameObject); inside the scope of if (other.CompareTag("Player"))
  • Spawn another potion by calling Spawn on your manager (to have it wait a few seconds before a spawning requires some more knowledge that you’ll get soon, but I don’t think it would be relevant to include it here).

Best of luck,
Simon