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!
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 :)
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).
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 !
I see the logic and I think that's by far the most accurate and simple example so far,thanks!
Your answer
Follow this Question
Related Questions
Static value “lost” in Unity when called on second script 0 Answers
Change skin on runtime 1 Answer
choose texture option when lookat 0 Answers
Runtime Normal Map Import Issues 1 Answer
Why does Nav Mesh Run Slower in build mode than in Editor? 1 Answer