• 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 sbjaviersantos · Aug 23, 2019 at 04:10 PM · instantiatescript.prefabsprefab-instance

How to instantiate a prefab declared in one script, from another script

T$$anonymous$$s is script one:


     public GameObject chatContent;
    //t$$anonymous$$s gameobject is referenced in the editor

     public void Start()
         {
             AddMessage("T$$anonymous$$s is ok");
         }
         void OnEnable()
         {
             EventSystemManager.StartListening("messageArrived", AddMessage);     
         }
         public void AddMessage(string message ) {
             print("One");
             GameObject remoteMessage = Instantiate(localMessagePrefab, chatContent.transform) as GameObject;
             print("Two" );
             remoteMessage.transform.GetC$$anonymous$$ld(0).GetComponentInC$$anonymous$$ldren<Text>().text = message;
             print("Three");
             DestroyMessage();
         }
 

alt text


The method AddMessage executed in Start method works nice, and also i can execute AddMessage several time in the script. The problem is that if i execute t$$anonymous$$s method AddMessage from another script it only get up to the print("One") in the method but the prefab is never instantiated. I have tried to do it by events and also referencing gameobject from the editor but not$$anonymous$$ng works. The method is executed but the instatiation fails with no errors.


T$$anonymous$$s is script two with events:

  private async void onMessageHandler(string e)
     {
         serverMessage = JsonUtility.FromJson<serverMssg>(e);
         EventSystemManager.TriggerEvent("messageArrived", serverMessage.normmsg);
         await miaApi.queueAnim(serverMessage.normmsg);
     }

TIA!

captureone.png (47.1 kB)
Comment
Add comment · Show 1
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 sacredgeometry · Aug 24, 2019 at 12:11 AM 0
Share

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by JonPQ · Aug 23, 2019 at 11:29 PM

if it prints one, but not two, there is an error on the line inbetween. Try putting a debug breakpoint on that line and debugging it to see what is going on. Its likely that your ChatContent or LocalMessagePrefab is null , has been destroyed somehow, or was null to start with. If you are using unity 2018/2019 and new prefab flow. make sure your references are actually being saved to the prefab, (they might look ok in scene, but might be null in the prefab)

Comment
Add comment · Show 16 · 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 sbjaviersantos · Aug 24, 2019 at 04:08 PM 0
Share
one.png (16.8 kB)
dos.png (25.3 kB)
avatar image sacredgeometry sbjaviersantos · Aug 24, 2019 at 04:11 PM 0
Share
avatar image sacredgeometry sbjaviersantos · Aug 24, 2019 at 04:25 PM 0
Share
avatar image sbjaviersantos sacredgeometry · Aug 24, 2019 at 04:29 PM 0
Share
Show more comments
Show more comments
avatar image sbjaviersantos · Aug 24, 2019 at 10:04 PM 0
Share
avatar image
0

Answer by YetAnotherKen · Aug 24, 2019 at 07:32 PM

Simple answer would be to either pass the reference to the object as a parameter to a method in the other script w$$anonymous$$ch stores it there, then that script can use it's reference. If you want to modify the original, you can pass it by reference using the ref keyword. Kind of like:

 method(ref objectReference);
 ...
 void method( ref ObjectType objectReference) {
    do stuff with objectReference.
 }
Comment
Add comment · Show 4 · 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 YetAnotherKen · Aug 24, 2019 at 07:37 PM 0
Share
avatar image sacredgeometry YetAnotherKen · Aug 24, 2019 at 08:29 PM 0
Share
avatar image sacredgeometry · Aug 24, 2019 at 07:39 PM 0
Share
avatar image sbjaviersantos · Aug 28, 2019 at 01:21 AM 0
Share

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

148 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to instantiate many different prefabs from one prefab? 1 Answer

How can you make a Instanciate prefab follow a scene object? 1 Answer

Instantiating Prefabs from a MonoBehaviour-derived class using an array -> compiling but not rendering [SOLVED] 0 Answers

Do I necessarily need to instantiate a prefab before putting it in List to conserve his variable data? 0 Answers

How to manipulate a variable of a prefab script (instantiated) while the game is runnning . 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