Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 Giokin · Jun 07, 2018 at 11:58 AM · runtimeinstantiate prefabchange variable

Changing prefab of a GameObject at runtime C#

Hello ! I'm new to unity and sorry if this is too simple but i've searched for some time without finding a solution, here's my problem: So I have a script using GameObject variable where it's defined the object my UI elements (Arrows etc.) elements will follow in game mode.I'm also using a button which instantiate a different Object and I'd like to assign that to the GameObject instead.Since it's runtime I cannot drag and drop something that's not created yet. Thanks in advance!

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 coidevoid · Jun 07, 2018 at 01:37 PM

If I understand correctly you want to have a reference of your instanciated GameObject in your script ? You said that you have a button wich instantiate GameObject, so why don't you add few lines in the button script like :

 private GameObject YourRef;
         
 public void ButtonMethod()
 {
      GameObject o = Instantiate(gameObject);
       YourRef = o;
 }

Is that what you need ? Have a good day :)

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 Giokin · Jun 07, 2018 at 03:10 PM 0
Share

Thank you for your time! I'm sorry as I might have explained what I'm after poorly, I would like to set any object i click on (for example) as the one object-prefab (which is connected to my script through editor) , would I be able to do so with this code? :)

Edit The script I mentioned is not on button but in the UI elements ins$$anonymous$$d(arrows).

avatar image coidevoid Giokin · Jun 07, 2018 at 06:01 PM 0
Share

Sorry man I keep struggle to get what you need.

So you have an GameObject wich is the Arrow and you have a script attached to it. You have in this script a GameObject type variable right ? And you want to assign it in run time still correct ?

If this is that the solution up here is the right one :) For exemple if you want that to happen when you click on the an item you can just say

 On$$anonymous$$ouseOver(){
 FindObjectOfType<Arrow>().refPrefab = this;
 }

and assign this to the objects that you want.. ?

Tell me if I'm on the good way to help you !

avatar image Giokin Giokin · Jun 08, 2018 at 04:45 AM 0
Share

I see the logic and I think that's by far the most accurate and simple example so far,thanks!

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

139 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 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

Static value “lost” in Unity when called on second script 0 Answers

Help (reloading a level) 0 Answers

Issues with video player coroutine 2 Answers

Instatiating and Copying Prefabs 0 Answers

UMA get access to Animator to set values 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