Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
0
Question by adrien.grs · Jun 25, 2015 at 12:59 PM · c#instantiateprefabscript.onclick

Instantiate prefab (button, texture) with attached script

Hey guys,

So here's my issue, I want to instantiate multiple prefabs with different components in the attached script and set the prefab child button Onclick to a function located in the script.

 GameObject prefab;
 public GameObject parentGameObject;

 void LoadPrefab() {

 prefab = (GameObject)Instantiate(Resources.Load("MyPrefab"));
 prefab.transform.SetParent(parentGameObject.transform);
 prefab.transform.localScale = new Vector3(1,1,1);
 prefab.AddComponent<MyScript>();
 prefab.AddComponent<MyScript>();
 prefab.GetComponent<MyScript>().DataInt = Data;
 prefab.GetComponent<MyScript>().DataVector = DataVector.point;
 prefab.transform.Find ("btn_goTo").GetComponent<Button> ().onClick.AddListener (delegate {prefab.GetComponent<MyScript>().FunctionInMyScript();}); }

The aim is : when the user click on the button, my camera is moving to the DataVector position. Now each prefab seems to have the same script attached... How can I fixed this ? Thanks in advance ! :)

alt text

prefab.png (47.8 kB)
Comment
Add comment · Show 6
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 Kirbyrawr · Jun 25, 2015 at 01:03 PM 0
Share

Can you explain better your problem please?, i didn't understand really well unfortunately :(

avatar image adrien.grs · Jun 25, 2015 at 02:06 PM 0
Share

I'm trying to load n prefabs (with 2 buttons as children) with a same script but different data in it (positions, variables...). In this script I have a function "$$anonymous$$ove" to move my camera to the prefab position. But when I have instantiated more than one prefab, the function "$$anonymous$$ove" move my camera to the same position (the one from the first prefab). I don't know if it's understandable now :/

avatar image jprocha101 · Jun 25, 2015 at 02:15 PM 0
Share

When in your script is the $$anonymous$$ove function executed? We may need to see more of your script to be able to help.

avatar image felixpk · Jun 25, 2015 at 02:19 PM 1
Share

So you have prefabs with buttons as children, right? You Instantiate these Prefabs all over your map. And when you click on a Prefabs button you want the camera to move to the prefabs position?

avatar image adrien.grs · Jun 26, 2015 at 11:07 AM 0
Share

That's right @felixpk.

Thank you @Hellium ! It works now :)

Show more comments

1 Reply

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

Answer by Hellium · Jun 25, 2015 at 02:44 PM

It seems you are adding your "MyScript" twice.

Are you sure the data you set to your script are different at least ? Add Debug.Log when changing the value !

By the way, it would be clearer and more efficient to write something like this :

 private GameObject newObject;
 public GameObject parentGameObject;

 void LoadPrefab()
 {
     MyScript myScript ;
     newObject = (GameObject)Instantiate(Resources.Load("MyPrefab"));
     myScript = newObject.AddComponent<MyScript>();
 
     newObject.transform.SetParent(parentGameObject.transform);
     newObject.transform.localScale = new Vector3(1,1,1);
 
     myScript.DataInt = Data;
     myScript.DataVector = DataVector.point;
     newObject.transform.Find ("btn_goTo").GetComponent<Button> ().onClick.AddListener ( delegate {myScript.FunctionInMyScript();} );
 }
 

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiation in C# 1 Answer

Instantiate prefab once help C# 1 Answer

Help with instantiating a prefab 1 Answer

How to make TCG Deck (Instatiate based on Prefab) 1 Answer

How do I make prefab follow a gameobject?(C#) 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