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 DrunkBearz · Jan 30, 2019 at 07:22 PM · networkinginstantiate prefab

How to properly instantiate GameObject from a factory class that is not in the scene?

How to properly instantiate GameObject by calling factory class that is not in the scene?

 public class BulletFactory {
 
   static private BulletFactory ref_this;
 
   public enum E_BULLET_TYPE { LINEAR, GRANADE }
 
   static public BulletFactory Get() {
     return (ref_this != null) ? ref_this : ref_this = new BulletFactory();
   }
 
   public void Create(E_BULLET_TYPE type, GameObject prefab, Vector3 pos, Vector2 velocity, int dir)
   {
     GameObject bullet = MonoBehaviour.Instantiate(prefab, pos, Quaternion.identity);
     if (type == E_BULLET_TYPE.LINEAR) {
       bullet.GetComponent<LinearBullet>().velocity = velocity;
       bullet.GetComponent<LinearBullet>().SetBulletDirection(dir);
     }
     else if (type == E_BULLET_TYPE.GRANADE) {
       bullet.GetComponent<GranadeBullet>().velocity = velocity;
       bullet.GetComponent<GranadeBullet>().SetBulletDirection(dir);
     }
   }
 }



This factory class is instantiating object which prefab it got through function parameters. When BulletFactory.Create() is called from some class that is in the scene(Monobehaviour class) everything works as expected. Only problem I have with this is that it gives me warning in console :

 The referenced script on this Behaviour (Game Object '<null>') is missing!
 UnityEngine.Object:Instantiate(GameObject, Vector3, Quaternion)
 The referenced script on this Behaviour (Game Object 'longBullet') is missing!
 UnityEngine.Object:Instantiate(GameObject, Vector3, Quaternion)


When doubleclicked warning references to this line:

 GameObject bullet = MonoBehaviour.Instantiate(prefab, pos, Quaternion.identity);



Is this the proper way of doing this and should I just ignore this warning?


Comment
Add comment · Show 4
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 KISP · Jan 30, 2019 at 09:34 PM 0
Share

Have you tried adding the [RuntimeInitializeOnLoad$$anonymous$$ethod] attribute? That might silence the warning.

avatar image DrunkBearz KISP · Jan 31, 2019 at 09:19 PM 0
Share

I tried but didn't work. Thanks for ideo tho.

avatar image toddisarockstar · Jan 31, 2019 at 12:52 AM 0
Share

I think you might be making things a bit more complicated than things need to be. why not $$anonymous$$eep your bulletfactory as a normal monobehavior script. Then simply make your Create function static? I'm not sure but the error looks like something you get when the name of your script in the inpector doesnt match the name of the class in your script. or if you somehow have this attached to a game object without being monobehavior?

avatar image DrunkBearz toddisarockstar · Jan 31, 2019 at 09:27 PM 0
Share

First of all I changed Create() function to be static and I'm not making instance oof bullet factory class which makes more sense so thanks for that advice. However warning is still there and when double clicked it points to "GameObject bullet = $$anonymous$$onoBehaviour.Instantiate(prefab, pos, Quaternion.identity);" so I guess is something to do with Instantiating game object from class that is not inheriting $$anonymous$$onobehavior and is not in the scene. Thanks for help, man :).

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by xxmariofer · Jan 31, 2019 at 10:41 PM

hello, can you test using Object.Instantiate rather than Monobehaviour.Instantiate? it didnt give me any warnings.

Comment
Add comment · Show 1 · 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 DrunkBearz · Feb 02, 2019 at 05:23 PM 0
Share

Nope, just tried it, same thing. What version of unity do you use?

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

142 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

Related Questions

Photon player instantiation BY MAGIC!? 0 Answers

Networking bad mouseposition for client 1 Answer

How to assign players a number during muliplayer 0 Answers

OnConnectedToServer, OnPlayerConnected, OnPlayerDisconnected not beeing called on instantiated prefab 0 Answers

How to setup a instantiated gameobject with mirror networking correctly? 2 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