- Home /
Calling a method from one prefab, calls the rest.
Hi, so when I call a method on one of the duplicates of a prefab, the rest gets called.. Which is not my desired behavior.. Here are my codes:
Spawns.cs:
namespace Tadget
{
using UnityEngine;
public class Spawn : Actions
{
public GameObject objToSpawn;
public Transform whereToSpawn;
/// Spawn objects depending on the field "objToSpawn"
public override void Use()
{
Instantiate(objToSpawn, whereToSpawn.position, whereToSpawn.rotation);
}
}
}
Destroy.cs:
namespace Tadget
{
using UnityEngine;
public class Destroy : Actions
{
public GameObject objToDestroy;
/// Spawn items depending on the field "objToDestroy"
public override void Use()
{
Destroy(objToDestroy);
}
}
}
Both are these derived from an abstract class that has an abstract method called Use()
.
This is where I call both use()
method
if (Input.touchCount > 0)
{
if (Input.GetTouch(0).phase == TouchPhase.Stationary || (Input.GetTouch(0).phase == TouchPhase.Moved && touch.deltaPosition.magnitude < maxMagnitude))
{
timeToOpenCounter -= Time.deltaTime;
InitializeUI();
if (timeToOpenCounter <= 0)
{
GetComponent<Spawn>().Use(); // <--- CALLED METHOD
GetComponent<Destroy>().Use(); // <---- CALLED METHOD
timeToOpenCounter = 0;
TurnUIVisible(false);
}
}
}
else
{
ResetTime();
}
TL;DR: Calling a method from one the duplicates of a prefab results in others being called as well. e.g. I want one egg to be broken, but the rest breaks, for some reason. That's the current situation I'm stuck in.
Answer by xxmariofer · Mar 04, 2019 at 01:49 PM
you are not calling scripts of instance of prefabs. you are accessing the scripts Spawn and Destroy on that same object so i would assume that each prefab have a Spawn script and Destroy script and that update method is called in every single prefab not only in once.