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 /
  • Help Room /
avatar image
0
Question by RickyX · Jan 13, 2016 at 09:17 PM · c#instantiateprefabvariableassign

Instantiate a prefab and assign it's values

I really need to assign the values of my prefab right after instantiating it. The source of my prefab is from assets, not from hierarchy. I do something like this: Code (csharp):

 public GameObject boxPrefab;
 public void InstantiateBox()
 {
         GameObject copy = (GameObject)Instantiate(boxPrefab, transform.position, Quaternion.identity);
         copy.GetComponent<TestBoxScript> ().varToAssign = gameObject;
 }

 

But the second line does not apply at all for some reason... The gameobject is instantiated but it's varToAssign does not change at all.

I really don't know how to approach my problem if this is not possible to do. And yes i tought of assigning the variable on Start() of my TestBoxScript but that's not gonna do what i want. I want my prefab to find it's belonger player, and if i did it from it's own script, it would never know which player it belongs to, if that makes sense. Also, this is not the original code i have, becouse i want my code to be private. But it's the same thing, it won't apply the second line in the function...

Comment
Add comment · Show 10
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 hulahoolgames · Jan 13, 2016 at 09:34 PM 0
Share

Just to make sure, the boxPrefab does have the TestBoxScript component attached to it? You don't get any NullReferenceExceptions on line 5?

avatar image RickyX · Jan 13, 2016 at 10:06 PM 0
Share

Nah, i wouldn't make a stupid mistake like that xD

avatar image hulahoolgames RickyX · Jan 13, 2016 at 10:15 PM 0
Share

Good to know! So since that is taken care of, do you do anything with the varToAssign on the Start/Enable of the TestBoxScript? This should technically be working fine, unless something else changes the variable in some other place.

avatar image RickyX hulahoolgames · Jan 13, 2016 at 10:32 PM 0
Share

Nope, and the variables are all null... I never made anything null in there through script so that means my code just didn't apply. Wierd... So friggen wierd...

Show more comments
avatar image brunocoimbra · Jan 13, 2016 at 11:01 PM 0
Share

Post your testBoxScript code so we can see if there is something to help we find out whats happening.

avatar image RickyX brunocoimbra · Jan 13, 2016 at 11:07 PM 0
Share
 public GameObject varToAssign;

that's all the code there is in it

avatar image brunocoimbra RickyX · Jan 13, 2016 at 11:12 PM 0
Share

and the rest of the file with the "InstantiateBox" method?

Show more comments

1 Reply

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

Answer by RickyX · Jan 14, 2016 at 05:19 PM

Okay guys i've figured my problem out. Thanks for taking your time anyways. The thing was, instantiate picks anything you give it, it doesen't need to be a GameObject, i think atleast. the reference that i got from my item database for the prefab was a transform, that's why it didn't apply my lines.

 Code (csharp):
 ItemDB.GetComponent<ItemDatabase>().items[itmID].itemDragDropPrefab

itemDragDropPrefab was a transform so yeah, it took that and spawned it, and i couldn't assign anything becouse i did clone.GetComponent instead of clone.gameObject.GetComponent So it was my fault. And the real code i had is here: http://forum.unity3d.com/threads/instantiate-a-prefab-and-assign-its-values.379447/

Comment
Add comment · Show 1 · 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 hulahoolgames · Jan 14, 2016 at 05:31 PM 0
Share

Glad you figured it out! That's why sometimes you need to look at more code to solve the issue :)

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

60 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

Related Questions

Instantiate a script into an instantiated prefab 3 Answers

[HELP C#] Making Gameobject 1 equal Prefab 2 1 Answer

Instantiate prefab if no prefab exists at location 1 Answer

Instantiated Object Has Wrong Rotation 0 Answers

If instantiate prefab is selected how can i change the color? 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