The question is answered, right answer was accepted
Script recieves previous given parameters when instantiating. Why?
I am trying to make a working buff system for my project. The system is based on the Pickup script and the Buff script, which is assigned to a BuffEffect GameObject. The logic of the Pickup script is that it instanciates the BuffEffect, gets component and gives the item's parameters (type, time of effect, value of effect), stored in the pickup script, to the BuffEffect. The BuffEffect checks the type by using if statements and makes required actions on the player accordingly. The Debug.Log rows in both scripts makes the Pickup script tell the parameters sent, and the Buff script name the parameters applied to the player. The pickup script works fine, sending the correct parameters. The problem is... After picking up the first item, the Buff script recieves a certain (not told to him) item, and every next pickup it recieves the PREVIOUS item parameters!
Here are the scripts:
public class Buff : MonoBehaviour {
[HideInInspector] public float value;
[HideInInspector] public float time;
private float timePassed;
private bool buffed;
[HideInInspector] public int type;
void Start ()
{
buffed = false;
}
void Update()
{
if (!buffed)
{
buffed = true;
if (type == 1)
{
// effect
Debug.Log("1 buff");
}
if (type == 2)
{
// effect
Debug.Log("2 buff");
}
if (type == 3)
{
// effect
Debug.Log("3 buff");
}
}
Debug.Log("Recieved parameters: " + player.name + " " + type + " " + time + " " + value);
}
}
And the Pickup part:
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Player")
{
Instantiate(buffObject);
buffObject.GetComponent<Buff>().type = buffType; Debug.Log("Type: " + buffType);
buffObject.GetComponent<Buff>().time = buffTime; Debug.Log("Time: " + buffTime);
buffObject.GetComponent<Buff>().value = buffValue; Debug.Log("Value: " + buffValue);
buffObject.GetComponent<Buff>().player = collision.gameObject; Debug.Log("Player: " + collision.gameObject.name);
Destroy(this.gameObject);
}
}
I have no idea what's the problem. Help please!
Answer by BaldBeardedMonk · Aug 30, 2017 at 05:17 PM
Instantiate the prefab as gameobject.
GameObject G = Instantiate(buffObject) as GameObject;
Follow this Question
Related Questions
Network.Instantiate passing parametrs to instantiated object, unassigned reference exception 0 Answers
Change color of object with C# 1 Answer
Gameobject array 0 Answers
IndexOutOfRangeException error when getting transform of array object 0 Answers
Spawn in a different and random Spawn point from a list 0 Answers