• 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 /
  • Help Room /
avatar image
0
Question by Ginjiruu · Apr 05, 2016 at 12:39 AM · instantiateprefab

Instantiating a prefab and some of the scripts are not working that are attached

I am trying to instantiate a prefab after a timer to create a wave spawning system for enemies. The problem is when I instantiate the prefab the enemy stats script works fine on it but then enemy AI script doesn't work and the prefabs sit there doing nothing. I looked up the issue and got a lot of people points towards using a prefab manager but I added one and the problem still persists.

 void SpawnEnemy (Transform _enemy)
 {
     Debug.Log("Spawing enemy: " + _enemy.name);

     Transform _sp = spawnpoints[Random.Range(0, spawnpoints.Length)];
     Instantiate(PrefabManager.Instance.EnemyPrefab, _sp.position, _sp.rotation);
 }

This is the code I'm using for spawning the enemy is there a better way of doing it that I'm mission out on?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by TBruce · Apr 05, 2016 at 01:11 AM

@Ginjiruu Have you tried recreating the prefab? Did you happen to modify a game object in your scene that was created from the prefab but the modifications were never applied? After instantiating the prefab, is it enabled? After instantiating the prefab, is the AI script active? Is the AI script active on the prefab itself?

Comment
Add comment · Show 4 · 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 Ginjiruu · Apr 05, 2016 at 05:51 PM 0
Share

Thank you very much for helping me out this fixed the problem of the enemies AI not working.However the enemies now are only running towards the origin of the scene and not updating to follow the player. Is this still to do with prefabs? If so thank you for the help and I'll close the question.

avatar image TBruce Ginjiruu · Apr 05, 2016 at 06:23 PM 0
Share

I am sorry but because I do not know what your AI script looks like I can not really answer that question. I can say however that the AI needs to continually do the following (say in Update or FixedUpdate): private GameObject player; void Start () { player = GameObject.FindGameObjectWithTag("Player").transform; }

 void Update ()
 {
     if (player)
     {
         // move enemy position closer to player position
         // the AI will need to take into account for anything blocking the player from the enemy (e.g walls)
     }
 }  
avatar image Ginjiruu TBruce · Apr 05, 2016 at 08:38 PM 0
Share

I have to player search function and it works perfectly fine when the prefab is placed in the scene via the inspector but when I use an instantiate function the script draws the line to the origin and moves the enemies but doesn't update to follow the player only leading towards the origin. Thank you very much for the help you have given so far as it has already fixed one of my problems.

avatar image Harry_Drew · Nov 10, 2018 at 12:44 PM 0
Share

Thank you this really helped.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

69 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

Related Questions

Create gameobject in other scene... 0 Answers

Place prefab with mouse on to a grid based surface with a correct orientation. c# 0 Answers

Can't Instantiate an object as child of another 1 Answer

Instantiating from different Sources causes collision inconsistency 0 Answers

How do I set a transform to a prefab? 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