Creating a GameObject or Component based off of an instanced Object












2















I am using factories to generate Objects, however I am having trouble figuring out how to add these created object instances to the unity scene.
If I add a component of the type, it's a new instance and doesn't have the created data.
If I try and instantiate, It won't work because it's a custom class and has no gameObject.



Is there a common way to do this? or do I have to redesign how this is working?



So far the only thing I can think of is creating an Initialize method in the objects that sets all the properties, and then after the object is created and generated, I create a new Component, and call the initialize method on the GetComponent that I just added, setting the values to the values from the generated object.
Seems like a headache and bad way to do it though.



Basically I'm generating Items, using some Randomization functions to get their rarity/itemType etc, and then creating said Item using factories as I won't know what type they are until the game is already running.
I'm, in theory, trying to generate everything without any need for a database, except a sprite > object connection. But, the more and more I get into this the more I think i'm going to have to have some sort of database.



Long story short, I need the instances in the Unity scene so that when a player clicks on them he can "equip them, disenchant them, etc"










share|improve this question

























  • Look here. You would add a new instance and than you can use reflection to copy all properties to the new instance. @AliKanat factory pattern

    – derHugo
    Nov 23 '18 at 5:17













  • Thank you @derHugo

    – Ali Kanat
    Nov 23 '18 at 9:00
















2















I am using factories to generate Objects, however I am having trouble figuring out how to add these created object instances to the unity scene.
If I add a component of the type, it's a new instance and doesn't have the created data.
If I try and instantiate, It won't work because it's a custom class and has no gameObject.



Is there a common way to do this? or do I have to redesign how this is working?



So far the only thing I can think of is creating an Initialize method in the objects that sets all the properties, and then after the object is created and generated, I create a new Component, and call the initialize method on the GetComponent that I just added, setting the values to the values from the generated object.
Seems like a headache and bad way to do it though.



Basically I'm generating Items, using some Randomization functions to get their rarity/itemType etc, and then creating said Item using factories as I won't know what type they are until the game is already running.
I'm, in theory, trying to generate everything without any need for a database, except a sprite > object connection. But, the more and more I get into this the more I think i'm going to have to have some sort of database.



Long story short, I need the instances in the Unity scene so that when a player clicks on them he can "equip them, disenchant them, etc"










share|improve this question

























  • Look here. You would add a new instance and than you can use reflection to copy all properties to the new instance. @AliKanat factory pattern

    – derHugo
    Nov 23 '18 at 5:17













  • Thank you @derHugo

    – Ali Kanat
    Nov 23 '18 at 9:00














2












2








2


1






I am using factories to generate Objects, however I am having trouble figuring out how to add these created object instances to the unity scene.
If I add a component of the type, it's a new instance and doesn't have the created data.
If I try and instantiate, It won't work because it's a custom class and has no gameObject.



Is there a common way to do this? or do I have to redesign how this is working?



So far the only thing I can think of is creating an Initialize method in the objects that sets all the properties, and then after the object is created and generated, I create a new Component, and call the initialize method on the GetComponent that I just added, setting the values to the values from the generated object.
Seems like a headache and bad way to do it though.



Basically I'm generating Items, using some Randomization functions to get their rarity/itemType etc, and then creating said Item using factories as I won't know what type they are until the game is already running.
I'm, in theory, trying to generate everything without any need for a database, except a sprite > object connection. But, the more and more I get into this the more I think i'm going to have to have some sort of database.



Long story short, I need the instances in the Unity scene so that when a player clicks on them he can "equip them, disenchant them, etc"










share|improve this question
















I am using factories to generate Objects, however I am having trouble figuring out how to add these created object instances to the unity scene.
If I add a component of the type, it's a new instance and doesn't have the created data.
If I try and instantiate, It won't work because it's a custom class and has no gameObject.



Is there a common way to do this? or do I have to redesign how this is working?



So far the only thing I can think of is creating an Initialize method in the objects that sets all the properties, and then after the object is created and generated, I create a new Component, and call the initialize method on the GetComponent that I just added, setting the values to the values from the generated object.
Seems like a headache and bad way to do it though.



Basically I'm generating Items, using some Randomization functions to get their rarity/itemType etc, and then creating said Item using factories as I won't know what type they are until the game is already running.
I'm, in theory, trying to generate everything without any need for a database, except a sprite > object connection. But, the more and more I get into this the more I think i'm going to have to have some sort of database.



Long story short, I need the instances in the Unity scene so that when a player clicks on them he can "equip them, disenchant them, etc"







c# unity3d






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 1:13









Draco18s

10.3k32041




10.3k32041










asked Nov 22 '18 at 20:21









HoldenHolden

678




678













  • Look here. You would add a new instance and than you can use reflection to copy all properties to the new instance. @AliKanat factory pattern

    – derHugo
    Nov 23 '18 at 5:17













  • Thank you @derHugo

    – Ali Kanat
    Nov 23 '18 at 9:00



















  • Look here. You would add a new instance and than you can use reflection to copy all properties to the new instance. @AliKanat factory pattern

    – derHugo
    Nov 23 '18 at 5:17













  • Thank you @derHugo

    – Ali Kanat
    Nov 23 '18 at 9:00

















Look here. You would add a new instance and than you can use reflection to copy all properties to the new instance. @AliKanat factory pattern

– derHugo
Nov 23 '18 at 5:17







Look here. You would add a new instance and than you can use reflection to copy all properties to the new instance. @AliKanat factory pattern

– derHugo
Nov 23 '18 at 5:17















Thank you @derHugo

– Ali Kanat
Nov 23 '18 at 9:00





Thank you @derHugo

– Ali Kanat
Nov 23 '18 at 9:00












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437598%2fcreating-a-gameobject-or-component-based-off-of-an-instanced-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437598%2fcreating-a-gameobject-or-component-based-off-of-an-instanced-object%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'