Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
Sign in to follow this  
Logystone

GameObjects spawning!

Question

Hello everyone

this is EnemySpawner script, first of all whats the problem with this code? nothing spawns and the second thing how can i spawn them in Canvas ? because they are UI Images

Please login or register to see this code.

 

Share this post


Link to post
Share on other sites

3 answers to this question

Recommended Posts

  • 1

There’s a few different ways to handle instantiated scripts accessing static scene scripts (or vice versa). Here is a couple options (but not limited to):

 

A: use static functions to carry out calls

 

B: using FindObjectOfType to find the gameManager script in the scene

 

C: have a script on the player that points to the gameManager. ie (player.gameObject.SendMessage and the player can tell the gameManager what it needs)

 

theres a million on ways to do it. But the jist is either statically call it, find the component or call a function on another object that points to the component you need

Share this post


Link to post
Share on other sites

  • 1

Your code is really hard coded - however it works. I tested it. I guess that you did a mistake setting enemies. In your code you use some defined values (Random.Range(0, 5); If you have less than 5 enemies it doesn't work when it selects an empty slot. So I recommend you to check the lenght of your array to set a limit - exactly like you do with your respawn spawn array. If you want to use a canvas element for each enemy, you have to use a canvas, but in World Space mode. You have many tutorials on YouTube about it. I wish you the best ++

 

PS : a clean code is the key to success :)

 

Please login or register to see this code.
  • Thanks 1

Share this post


Link to post
Share on other sites
  • 0

Thank you very much! it worked finally but some of the Instantiated gameobjects  go up and down and each enemy has this script talk a look at "enemySpeed" and the reason there's "-"before "enemySpeed" that because if i didn't put "-" it go up auto.., and there's another problem about GameManager script i'm trying to access to this script but when it Instantiated i get this error on trigger : "NullReferenceException: Object reference not set to an instance of an object
EnemyManager.OnTriggerEnter2D (UnityEngine.Collider2D player)
"

Please login or register to see this code.

NOTICE(in the pref i attached the script but after instantiate it de-attach auto..) i don't know if there's a better way to access to this script without getting errors

and i'm sorry if i must create another topic for this error. Thank you all! 

 

(NOTICE!!!: About the Instantiated gameobjects  that go up and down i fixed it i made a dumb mistake nvm 😂)

Edited by Logystone
my brain forgot to type something important...

Share this post


Link to post
Share on other sites
Guest
This topic is now closed to further replies.
Sign in to follow this  

×
×
  • Create New...