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

This is script one:


     public GameObject chatContent;
    //this gameobject is referenced in the editor

     public void Start()
         {
             AddMessage("This 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.GetChild(0).GetComponentInChildren<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 this 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 nothing works. The method is executed but the instatiation fails with no errors.


This 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

In principal it should work fine. Just check through your code. Debug the execution and step through to see what happens at the line you believe to be failing.

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

First of all thanks for the reply. Im using unity 2018.4.6f1. I have check all the references and all seem to be ok (also in prefab). Later i tried to debug with visual studio (breakpoint) but im not really sure what im looking for ( I am not used to using it), so i just comparing the same breakpoint from start method (that works) and from the eventTrigger.


So for me it looks like nothing is exactly null (only remote$$anonymous$$essage but it is ok), but maybe it is not getting the exactly same objetcs (It is my conclusion since the value is not exactly the same).

Also some things are in red but im not sure what it means.

Start method debug alt text


EventTrigger Debug alt text

one.png (16.8 kB)
dos.png (25.3 kB)
avatar image sacredgeometry sbjaviersantos · Aug 24, 2019 at 04:11 PM 0
Share

Ok so what you are looking at is the the current state of variables that exist in that scope and the call stack.

Did you stop before line 14? in the first code example as the remote message is null.

avatar image sacredgeometry sbjaviersantos · Aug 24, 2019 at 04:25 PM 0
Share

@sbjaviersantos

Video

avatar image sbjaviersantos sacredgeometry · Aug 24, 2019 at 04:29 PM 0
Share

Sorry i forgot to upload the image with breakpoint , i exactly stop in line 14.

Show more comments
Show more comments
avatar image sbjaviersantos · Aug 24, 2019 at 10:04 PM 0
Share

@JonPQ so far, the only thing that i found strange is that the transform that i use to instantiate the prefab is gone, it does not appear in debug mode and also if I try to print transform.position (for example) nothing happens, even no errors. If I do it from the same script (several time in diferent moments) it works but if I launch the event trigger or execute the method by reference from another script it doesnt. I also tried to attach both scripts in the same object but there was no difference. $$anonymous$$ay is a behavior of unity , but i dont get it....

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

Oh, the other method I was thinking of was having it, and the class it is in public so that the other class can declare a reference and use it, kind of like:

 ObjectType objectType = OtherObject.GetComponent<ObjectType>();
 
 void method () {
   do stuff with objectType.objectReference.  }
avatar image sacredgeometry YetAnotherKen · Aug 24, 2019 at 08:29 PM 0
Share

i.e.

     public class TestObj
     {
         public string Name { get; set; }
     }
 
     class Program
     {
         static void ChangeName(TestObj obj)
         {
             obj.Name = "New Name";
         }

         static void $$anonymous$$ain(string[] args)
         {
             var testA = new TestObj {
                 Name = "Some name"
             };
 
             ChangeName(testA);
 
             Console.WriteLine(testA.Name);
         }

Writes: "New Name"

... to console, not "Some name"

avatar image sacredgeometry · Aug 24, 2019 at 07:39 PM 0
Share

You dont need to use ref if its a class which it probably is. Its passed by reference anyway.

avatar image sbjaviersantos · Aug 28, 2019 at 01:21 AM 0
Share

I think is not exactly what i need.... my case is like:

I receive a message via websocket (it works) Later i pass this message to a "Chat" in other script/object and the message is passed, the problem is that the object "Chat" doesnt instance the prefab is like it doesnt exist but i does because i can instance the same prefab from itself in the same "play".

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

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

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

Something wrong with my project 0 Answers

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


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