Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 24, 2020 at 03:32 PM by nikhilVardhan for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by nikhilVardhan · Jun 24, 2020 at 07:16 AM · scripting problemscripting beginnercall

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
0
Best Answer

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]); } } }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nikhilVardhan · Jun 24, 2020 at 07:47 AM 0
Share

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.

avatar image UnityedWeStand nikhilVardhan · Jun 24, 2020 at 07:59 AM 0
Share

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?

avatar image nikhilVardhan UnityedWeStand · Jun 24, 2020 at 08:33 AM 0
Share

All functions are working correctly, i am sure of it. I checked them using print statements, like Debug.Log.

Show more comments

Follow this Question

Answers Answers and Comments

237 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Error while creating an object 0 Answers

How to tell the game if the player is not moving 1 Answer

How to increment value of axis when something happens 2 Answers

How to toggle on and off an animation with the same key? 1 Answer

How can I reset a sequence or order using Integers? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges