Question by
Journal2classe · Sep 06, 2020 at 06:15 PM ·
scripting problemprogramming
How can I use the same script for multiple gameobjects?
So I have a problem. When I put a value in the inspector to give xp points for my player the xp gain is not the good value it give one of value what I put in one of other scripts. I don't know if you understand what I mean because my english is not perfect but I esper you can help me.
using UnityEngine;
public class TakeToEat : MonoBehaviour
{
private Collider2D foodCol;
private Vector2 initPos;
private bool canMove = false;
public int foodPrize;
public float xpGain;
void Start()
{
foodCol = GetComponent<Collider2D>();
initPos = transform.position;
}
void Update()
{
if(Input.touchCount > 0)
{
Touch touch = Input.GetTouch(0);
Vector2 touchPosition = Camera.main.ScreenToWorldPoint(touch.position);
Collider2D touchedCollider = Physics2D.OverlapPoint(touchPosition);
if(touch.phase == TouchPhase.Began)
{
if (foodCol == touchedCollider)
{
canMove = true;
TouchAndCuddle.instance.canCuddle = false;
}
}
if(touch.phase == TouchPhase.Moved)
{
if (canMove)
{
gameObject.transform.position = touchPosition;
}
}
if (touch.phase == TouchPhase.Ended)
{
if (TouchAndCuddle.instance.canEat)
{
XpBar.instance.AddXp(xpGain);
Debug.Log(xpGain);
}
canMove = false;
transform.position = initPos;
TouchAndCuddle.instance.canEat = false;
TouchAndCuddle.instance.canCuddle = true;
}
}
}
}
Comment
Answer by BenWiller1989 · Sep 06, 2020 at 07:21 PM
Give multiple Objects the same Script... could you explain your issue?
Your answer
Follow this Question
Related Questions
Moving script wont even move enemy no more? 0 Answers
Scripting an AI similar to Double Dragon/Final Fight 0 Answers
Input Command Issues 0 Answers
Unity HUD Tracking and Target Selection 0 Answers