- Home /
The question is answered, right answer was accepted
Unable to call a function from one script, but working fine if i call another one from another script.
I know there are lots of questions have been asked on this topic and i think i have surfed through all of them but i am still unable to figure out. I have used almost all of the methods from forums and from asked questions.
Below is the script:
public class Player : MonoBehaviour { public GameObject enemySpawner; public GameObject testing; enemySpawn destroyed; Destroy dest; int counting;
private void Start()
{
destroyed = enemySpawner.GetComponent<enemySpawn>();
dest = testing.GetComponent<Destroy>();
}
void Update()
{
transform.Translate(Input.GetAxis("Horizontal"), Input.GetAxis("Vertical"),0);
}
private void OnTriggerEnter(Collider collider)
{
if (collider.CompareTag("Quarter1"))
{
Debug.Log("Entered");
// spawnEnable = false;
dest.check();
destroyed.destroyerFun();
}
// spawnEnable = true;
}
}
"testing" is a simple 3d game object that i am using to check if my method is wrong or not, it has a script attached Destroy, and i want to call a function "check". While "enemySpawner" is the script attached to a prefab which i want to access and to call a function "destroyerFun".
And i want it to happen on trigger as you can see. But the function "check" is called while "destroyFun" does not.
Destroy Script:
public class Destroy : MonoBehaviour { public void check() { Debug.Log("Hye"); } }
enemySpawn script:
public class enemySpawn : MonoBehaviour { public GameObject enemySpawnee; GameObject tempSpawn; float xPos, yPos;
bool spawnEnable = true;
[HideInInspector]
public GameObject[] holdSpawn;
[HideInInspector]
public int actualCount = 0;
private void Awake()
{
if (spawnEnable)
{
while (actualCount < 5)
{
xPos = Random.Range(-65, -3);
yPos = Random.Range(22, 3);
tempSpawn = Instantiate(enemySpawnee, new Vector3(xPos, yPos, 10), Quaternion.identity) as GameObject;
actualCount++;
}
if (actualCount == 5)
{
spawnEnable = false;
}
}
}
void Start()
{
// StartCoroutine(enemySpawning());
holdSpawn = GameObject.FindGameObjectsWithTag("Enemy");
Debug.Log("From enemy spawn" + holdSpawn.Length);
}
public void destroyerFun()
{
for(int i = 0; i < holdSpawn.Length; i++)
{
Destroy(holdSpawn[i]);
}
}
/ IEnumerator enemySpawning() { yield return new WaitForSeconds(0.2f); }/
}
Please ignore comments and other things since i am trying to solve it and doing this and that stuff.
Please help me and try to tell me every method in single comment/ reply because i don't know why my questions are going under moderation.
@Hellium @Bunny83 @Eric5h5 @robertbu @aldonaletto @tanoshimi @whydoidoit @clunk47 @Statement @fafase
Answer by nikhilVardhan · Jun 24, 2020 at 03:30 PM
I did it, i am able delete the enemies by instantiating them in awake function in enemySpawn script and storing the spawned objects in a array in player script using GameObject.FindObjectsWithTag("Tag here") and then deleted it using for loop.
Player Script:
public GameObject[] enemySpawner; private void Start() { enemySpawner = GameObject.FindGameObjectsWithTag("Enemy"); } private void OnTriggerEnter(Collider collider) { if (collider.CompareTag("Quarter1")) { for (int i = 0; i < enemySpawner.Length; i++) { Destroy(enemySpawner[i]); } } }
Answer by UnityedWeStand · Jun 24, 2020 at 07:40 AM
My guess is that running into this problem because you are calling destroyerFun() on the prefab itself vs an instantiated copy of the prefab. If you do not instantiate a prefab, its Start() and Awake() functions are never called. This causes the variable holdSpawn to not be set in the Start() function, which will cause the script to skip everything in the for loop in destroyerFun().
Try testing this setup with an instantiated copy of the object with the enemySpawn script and see if it solves the problem.
I have instantiated the object and there are five objects, that are spawned using it. I am destroying the spawned objects when player collider enters the quarter1 trigger.
Have you verified that the function itself isn't being executed, either through breakpoint debugging or adding a print statement at the beginning of the function?
All functions are working correctly, i am sure of it. I checked them using print statements, like Debug.Log.