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 BigDakaFa7 · Jul 01, 2014 at 05:26 AM · c#instantiateprefab

How do i Instantiate a prefab with specific assests included

I have a question on how to have a prefab that is instantiated have an assest attached to a script that is in the game world already. So basically the prefab gets spawned in and in one of the scripts it needs a assest to be pointed too but when it spawns it doesnt have the value and i want it to point to a prefab already in the game world how would i go about doing this in c# Thank you

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
0

Answer by Kiwasi · Jul 01, 2014 at 07:02 AM

You can build any links you need in Awake() and Start().

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 Andres-Fernandez · Jul 01, 2014 at 07:10 AM

I suggest 2 different approaches:

  1. Have the link in the object that spawns the prefab. That way the spawner will have a reference to the spawned object and can set the link from it's own link.

  2. http://docs.unity3d.com/ScriptReference/GameObject.Find.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 Yokai · Jul 01, 2014 at 07:14 AM

I'm not really sure what you're trying to do.

Are you trying to pass an existing GameObject as a variable for your script? If so I suppose the easiest way would be to look for a correct object in the scene and then set the variable during Start() or OnEnable() like so

 GameObject go;
 void OnEnable()
 {
     // This attaches the first object of type T found on your scene to the go var
     go= GameObject.FindObjectOfType<T>();
 }

Or do you want to have your instantiated object be a child of a GameObject on the scene? If so you need to use Transform.parent{set}:

 // You still need a correctly set GameObject var
 void OnEnable()
 {
     transform.parent= go.transform;
     
     // Also set position, rotation and scale to new parent
     transform.position= Vector3.zero;
     transform.rotation= Quaternion.identity;
     transform.localScale= Vector3.zero;
 }


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

24 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

Related Questions

Issue Instantiating prefab in C# 2 Answers

Destroying Objects. 1 Answer

C# Instantiating a Gameobject with a Flat Sphere Collider 1 Answer

Null Reference Exception on instantiated object's script. 2 Answers

,Instantiating Prefab in C# 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