Home > Mobile >  How can i reference the player to get his/her position to be able to drop a item in front of the pla
How can i reference the player to get his/her position to be able to drop a item in front of the pla

Time:10-01

Hey i was wondering if someone could help me getting the transform of a player then taking that and making a item drop in frount of the player i keep getting this error in unity saying "the name 'inFrontOfPlayer' does not exist in current context im also referencing the player with a

private Transform player;

This is the code if this is helpful or not

    public void CustomStart() 
    {
        defaultSprite = GetComponent<Image>().sprite;
        amountText = transform.GetChild(0).GetComponent<Text>();
        player = GameObject.FindWithTag("Player").transform;
    }

    private void Update() 
    {
        Vector3 inFrontOfPlayer = player.position   player.forward * 1;
    }

    public void DropItem()
    {
        if(slotsItem)
        {
            slotsItem.transform.parent = null;
            slotsItem.gameObject.SetActive(true);
            slotsItem.transform.position = inFrontOfPlayer;
        }
    }

CodePudding user response:

the inFrontOfPlayer variable is placed within the Update() function's scope, meaning that once the function ends the variable is gone, and nothing else outside that scope (the curly brackets) can access it. The way to fix this is to declare the variable earlier in the file, like private Vector3 inFrontOfPlayer at the start , and then using inFrontOfPlayer = player.position player.forward * 1; in the update function. This means that the variable is now no longer in the scope of the update function meaning that it can be accessed by functions in the same class (or MonoBehaviour) Example:

 public Transform player;
 private Vector3 inFrontOfPlayer;
 public void CustomStart() 
    {
        defaultSprite = GetComponent<Image>().sprite;
        amountText = transform.GetChild(0).GetComponent<Text>();
        player = GameObject.FindWithTag("Player").transform;
    }

    private void Update() 
    {
        inFrontOfPlayer = player.position   player.forward * 1;
    }

    public void DropItem()
    {
        if(slotsItem)
        {
            slotsItem.transform.parent = null;
            slotsItem.gameObject.SetActive(true);
            slotsItem.transform.position = inFrontOfPlayer;
        }
    }

CodePudding user response:

Here you declare a class-level field:

private Transform player;

This field is available to any instance method in the class. For example, you set a value to the field here:

public void CustomStart() 
{
    //...
    // Here:
    player = GameObject.FindWithTag("Player").transform;
}

But here you're declaring and setting a local variable in a method:

private void Update() 
{
    Vector3 inFrontOfPlayer = player.position   player.forward * 1;
}

Then the method ends and the variable and its value are discarded and never used. If you want it to remain as a part of the class after the method ends and be available to other methods on that instance, make it a class-level field just like your other class-level fields:

private Vector3 inFrontOfPlayer;

// ...

private void Update() 
{
    inFrontOfPlayer = player.position   player.forward * 1;
}
  • Related