Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by michael_rod45 · Oct 20, 2017 at 02:58 AM · instantiateprefabinstantiate prefab

I am trying to instantiate a new enemy into the scene, then change the state of that enemy by calling to a function in its script, any critique on how I could go about it or fix it? Am I missing something?

GameObject newEnemy = Instantiate (enemy, spawnPoints[i].position, spawnPoints[i].rotation);

newEnemy.gameObject.GetComponent ().attackNOW();

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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by fafase · Oct 20, 2017 at 07:37 AM

First you need to cast the Instantiate as it returns Object. With the GameObject reference you can use GetComponent with the component type. That would be the class holding the AttackNOW method.

 GameObject newEnemy = (GameObject)Instantiate (enemy, spawnPoints[i].position, spawnPoints[i].rotation);
 newEnemy.GetComponent<ScriptWithAttackNOWMethod> ().attackNOW();
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 unit_nick · Oct 20, 2017 at 04:39 AM

Assuming the first line works as intended

 GameObject newEnemy = Instantiate (enemy, spawnPoints[i].position, spawnPoints[i].rotation);

then to call a function on that object you simply call the function

 newEnemy.attackNOW();

Comment
Add comment · Show 2 · 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 pako · Oct 20, 2017 at 06:57 AM 1
Share

@unit_nick what you say would only be possible if GameObject had an attackNOW() method, which it doesn't. Hence the need for GetComponent ("Custom Type").

avatar image unit_nick pako · Oct 20, 2017 at 07:04 AM 0
Share

true. I missed that because I didn't really look at it. so yeah you would make sure newEnemy is created as the correct type of object

 Enemy newEnemy = (Enemy) Instantiate (enemy, spawnPoints[i].position, spawnPoints[i].rotation);


avatar image
0

Answer by Harinezumi · Oct 20, 2017 at 08:47 AM

(This is more a comment to @fafase's answer, but I wanted it to have nice formatting)

Actually, you don't need to cast to GameObject, Instantiate will return a type of the first parameter. Because of this if I only need that type, what I usually do:

 [SerializeField]
 private Enemy enemyPrefab = null; // set reference from Editor
 
 // within the enemy spawning function
 Enemy newEnemy = Instantiate(enemyPrefab, spawnPoints[i].position, spawnPoints[i].rotation); // because enemyPrefab has type Enemy, you can directly assign without casting
 newEnemy.attackNOW(); // note that this will execute after Enemy.Awake(), but before Enemy.Start()

If for any reason you need to work with other components on newEnemy, then of course you need GetComponent().

Comment
Add comment · Show 3 · 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 fafase · Oct 23, 2017 at 07:49 PM 0
Share

For the generic version it does. The docs is quite clear about it, Object.Instantiate returns Object.

https://docs.unity3d.com/ScriptReference/Object.Instantiate.html

avatar image unit_nick fafase · Oct 24, 2017 at 03:26 AM 0
Share

Yeah I wasn't sure if I should comment on this or not. From the way it is worded it sounds like it is something @Harinezumi does do and that it works. But nowhere can I find it documented. So although it may work should you do it? This is the sort of thing that breaks with updates.

avatar image Harinezumi unit_nick · Oct 24, 2017 at 06:56 AM 0
Share

It's because I don't have a reference to GameObject enemyPrefab, I use directly a reference to an Enemy script (if the prefab has the script, you can assign it), so when I call Instantiate(enemyPrefab, ...) it is not using GameObject.Instantiate(Object ...), it can infer that from the type of enemyPrefab that I want to use GameObject.Instantiate(...). Here is an answer that explains it.

The script reference does very basic explanations in certain cases, but in practice there are a lot of completely legal shortcuts (it is slightly faster if you avoid the cast).

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.

Follow this Question

Answers Answers and Comments

104 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

Related Questions

Why is it important to create an empty gameobject for my prefabs? 0 Answers

How to add gameobjects to opened prefab with EditorWIndow? 0 Answers

Instantiate prefabs next to each other? 1 Answer

prefab not loading with instantiate 2 Answers

How to change Image on button click in a prefab that is instantiated 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