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 /
avatar image
1
Question by Zombie_Socks_88 · Dec 01, 2012 at 12:23 AM · gameobjectprefabvariablebeginner

Prefab problem, wont accept gameObject in var

Ok this is something I haven't run into yet. I tried looking on here for similar situations to find nothing. Here is basically what is happening. I have an object called "Enemy" and has its own script called "EnemyScript." In the script I have a variable called "player" that is GameObject type, so the enemy can look for the player object. So in the inspector I dragged and dropped the player object into it, and it works completely fine.

So I am trying to create a prefab for the enemy (I think this will solve my other problem on enemy spawning, where when I kill the "Enemy" object it stops spawning them). I create the prefab and drag and drop the "Enemy" object into the prefab but it deletes the player GameObject out of the var in the inspector for it, and it wont let me place it back in.

Hopefully that is understandable, if not I can try and make it ore clear.

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
1

Answer by Democre · Dec 01, 2012 at 03:55 AM

You cannot have scene objects in prefab members.

Once you instantiate them, then you can assign them through code. Or, if you have added the prefab to your scene, you can assign it as you did before.

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 Zombie_Socks_88 · Dec 01, 2012 at 05:02 AM 0
Share

Im not sure what you mean, or how exactly how to do that. Would it help if I posted my code(s) and script(s) for my enemy stuff. I have a script for the enemy(used to make the enemy look and chase the player) and one for spawning the enemies around the scene.

Heck maybe by showing that info, I might not even need to create a prefab as ultimately Im trying to make it that when I kill the original enemy, it wont stop spawning the others. I get some kind of message that its trying to call a transform that has been destroyed.

avatar image redline2466 · Dec 07, 2016 at 01:37 AM 0
Share

Why can't you have them?

avatar image getyour411 redline2466 · Dec 07, 2016 at 03:45 AM 0
Share

Prefabs could be instantiated anywhere by code, including a different scene that does not contain a GameObject previously assigned: NullRefs.

avatar image
0

Answer by redline2466 · Dec 07, 2016 at 05:43 AM

http://answers.unity3d.com/questions/353955/assign-a-gameobject-through-script.html

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 AdamtheNoob · Dec 07, 2016 at 05:43 AM

You could either

A - Use another method to select the player GameObject

 GameObject player;
 
 Start() {
     player = GameObject.Find("Player"); // Name of the player object
 }

B - Create a prefab for the playerObject

This method allows you to add the player to the enemy prefabs

If the object is not a prefab, the editor will not know if you will put it into every single scene, as such it doesn't allow you to add them to prefabs, which can be added at any time into the scene (including during runtime). Because of that it simply doesn't allow you to do that.

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

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

14 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

Related Questions

How do I assign a prefab reference to a private variable? 1 Answer

Setting a game object from a list to be the active character that gets instantiated 1 Answer

What is the type of my prefab? 2 Answers

Set Rigidbody variable to Prefab using code 1 Answer

Disable/Enable game object by clicking instantiated prefab during runtime 0 Answers


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