Instantiating gameObject based on Type












2














So basically I'm struggling with a problem and was hoping someone here could point me in the right direction.



Im randomly generating Items, and am trying to instantiate said items, the only problem is while I'm able to instantiate the parent item, I won't know up front if the generated item will be a "weapon/armor" etc, which are all inherited children of Item. So I am able to get the Type of the generated item, but am having trouble converting that Type back to said Script/Object to be instantiated.



For example :
Item > Weapon > Sword.
I am able to generate a sword, and get the type of the sword, however I am unable to instantiate a sword, because I only have the Type and not an actual Sword.



Since my Sword class has data loaded during it's Start method, then Weapon, then Item, I cannot just instantiate an Item and know it's a sword.
Perhaps I'm doing this all wrong.



Thank you for reading!










share|improve this question


















  • 1




    1) Factories ("make me a thing") and 2) Item Registries ("find me the pattern for a thing I know the name of").
    – Draco18s
    Nov 21 at 0:36












  • Thank you! I'm reading on factories right now. Do you have any simple examples or links?
    – Holden
    Nov 21 at 0:41










  • @Draco18s I'm not sure how to mark your comment as a great answer, but thank you very much. Teaching to fish is very important in this field and I'm learning a lot, I just didn't know where to begin. Factories look great!
    – Holden
    Nov 21 at 1:00






  • 1




    @Holden take a look into scriptable objects you can create a database and then have the lookup pretty quickly. SO's have been around for awhile and they are very nice for this particular situation.
    – Levon Ravel
    Nov 21 at 1:24










  • @LevonRavel Thanks! I will do some reading on these as well =)
    – Holden
    Nov 21 at 4:33
















2














So basically I'm struggling with a problem and was hoping someone here could point me in the right direction.



Im randomly generating Items, and am trying to instantiate said items, the only problem is while I'm able to instantiate the parent item, I won't know up front if the generated item will be a "weapon/armor" etc, which are all inherited children of Item. So I am able to get the Type of the generated item, but am having trouble converting that Type back to said Script/Object to be instantiated.



For example :
Item > Weapon > Sword.
I am able to generate a sword, and get the type of the sword, however I am unable to instantiate a sword, because I only have the Type and not an actual Sword.



Since my Sword class has data loaded during it's Start method, then Weapon, then Item, I cannot just instantiate an Item and know it's a sword.
Perhaps I'm doing this all wrong.



Thank you for reading!










share|improve this question


















  • 1




    1) Factories ("make me a thing") and 2) Item Registries ("find me the pattern for a thing I know the name of").
    – Draco18s
    Nov 21 at 0:36












  • Thank you! I'm reading on factories right now. Do you have any simple examples or links?
    – Holden
    Nov 21 at 0:41










  • @Draco18s I'm not sure how to mark your comment as a great answer, but thank you very much. Teaching to fish is very important in this field and I'm learning a lot, I just didn't know where to begin. Factories look great!
    – Holden
    Nov 21 at 1:00






  • 1




    @Holden take a look into scriptable objects you can create a database and then have the lookup pretty quickly. SO's have been around for awhile and they are very nice for this particular situation.
    – Levon Ravel
    Nov 21 at 1:24










  • @LevonRavel Thanks! I will do some reading on these as well =)
    – Holden
    Nov 21 at 4:33














2












2








2







So basically I'm struggling with a problem and was hoping someone here could point me in the right direction.



Im randomly generating Items, and am trying to instantiate said items, the only problem is while I'm able to instantiate the parent item, I won't know up front if the generated item will be a "weapon/armor" etc, which are all inherited children of Item. So I am able to get the Type of the generated item, but am having trouble converting that Type back to said Script/Object to be instantiated.



For example :
Item > Weapon > Sword.
I am able to generate a sword, and get the type of the sword, however I am unable to instantiate a sword, because I only have the Type and not an actual Sword.



Since my Sword class has data loaded during it's Start method, then Weapon, then Item, I cannot just instantiate an Item and know it's a sword.
Perhaps I'm doing this all wrong.



Thank you for reading!










share|improve this question













So basically I'm struggling with a problem and was hoping someone here could point me in the right direction.



Im randomly generating Items, and am trying to instantiate said items, the only problem is while I'm able to instantiate the parent item, I won't know up front if the generated item will be a "weapon/armor" etc, which are all inherited children of Item. So I am able to get the Type of the generated item, but am having trouble converting that Type back to said Script/Object to be instantiated.



For example :
Item > Weapon > Sword.
I am able to generate a sword, and get the type of the sword, however I am unable to instantiate a sword, because I only have the Type and not an actual Sword.



Since my Sword class has data loaded during it's Start method, then Weapon, then Item, I cannot just instantiate an Item and know it's a sword.
Perhaps I'm doing this all wrong.



Thank you for reading!







c# unity3d






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 at 0:34









Holden

607




607








  • 1




    1) Factories ("make me a thing") and 2) Item Registries ("find me the pattern for a thing I know the name of").
    – Draco18s
    Nov 21 at 0:36












  • Thank you! I'm reading on factories right now. Do you have any simple examples or links?
    – Holden
    Nov 21 at 0:41










  • @Draco18s I'm not sure how to mark your comment as a great answer, but thank you very much. Teaching to fish is very important in this field and I'm learning a lot, I just didn't know where to begin. Factories look great!
    – Holden
    Nov 21 at 1:00






  • 1




    @Holden take a look into scriptable objects you can create a database and then have the lookup pretty quickly. SO's have been around for awhile and they are very nice for this particular situation.
    – Levon Ravel
    Nov 21 at 1:24










  • @LevonRavel Thanks! I will do some reading on these as well =)
    – Holden
    Nov 21 at 4:33














  • 1




    1) Factories ("make me a thing") and 2) Item Registries ("find me the pattern for a thing I know the name of").
    – Draco18s
    Nov 21 at 0:36












  • Thank you! I'm reading on factories right now. Do you have any simple examples or links?
    – Holden
    Nov 21 at 0:41










  • @Draco18s I'm not sure how to mark your comment as a great answer, but thank you very much. Teaching to fish is very important in this field and I'm learning a lot, I just didn't know where to begin. Factories look great!
    – Holden
    Nov 21 at 1:00






  • 1




    @Holden take a look into scriptable objects you can create a database and then have the lookup pretty quickly. SO's have been around for awhile and they are very nice for this particular situation.
    – Levon Ravel
    Nov 21 at 1:24










  • @LevonRavel Thanks! I will do some reading on these as well =)
    – Holden
    Nov 21 at 4:33








1




1




1) Factories ("make me a thing") and 2) Item Registries ("find me the pattern for a thing I know the name of").
– Draco18s
Nov 21 at 0:36






1) Factories ("make me a thing") and 2) Item Registries ("find me the pattern for a thing I know the name of").
– Draco18s
Nov 21 at 0:36














Thank you! I'm reading on factories right now. Do you have any simple examples or links?
– Holden
Nov 21 at 0:41




Thank you! I'm reading on factories right now. Do you have any simple examples or links?
– Holden
Nov 21 at 0:41












@Draco18s I'm not sure how to mark your comment as a great answer, but thank you very much. Teaching to fish is very important in this field and I'm learning a lot, I just didn't know where to begin. Factories look great!
– Holden
Nov 21 at 1:00




@Draco18s I'm not sure how to mark your comment as a great answer, but thank you very much. Teaching to fish is very important in this field and I'm learning a lot, I just didn't know where to begin. Factories look great!
– Holden
Nov 21 at 1:00




1




1




@Holden take a look into scriptable objects you can create a database and then have the lookup pretty quickly. SO's have been around for awhile and they are very nice for this particular situation.
– Levon Ravel
Nov 21 at 1:24




@Holden take a look into scriptable objects you can create a database and then have the lookup pretty quickly. SO's have been around for awhile and they are very nice for this particular situation.
– Levon Ravel
Nov 21 at 1:24












@LevonRavel Thanks! I will do some reading on these as well =)
– Holden
Nov 21 at 4:33




@LevonRavel Thanks! I will do some reading on these as well =)
– Holden
Nov 21 at 4:33












1 Answer
1






active

oldest

votes


















2














Create a weapons enum



public enum WeaponType {sword, crossbow, q36SpaceModulator }


Use that to tell your factory (@Draco18s comment) what to build - methods for creating weapons in this case.



public class WeaponFactory {
public Weapon Create (WeaponType thisWeapon) {
Weapon newWeapon;

switch (thisWeapon) {
case WeaponType.sword :
newWeapon = BuildSword();
break;

case WeaponType.crossbow:
newWeapon = BuildCrossbow();
break;

// and so on...
default: // we don't have whatever-you-asked-for
}

return newWeapon;
}

private Weapon BuildSword() { return new Sword(); }

}

// somewhere in code...
// you may have other parameters for particular weapon attributes
var sword = royalArmory.Create(Weapon.sword);





share|improve this answer





















  • I really like this concept, however at time of needing to create a weapon, I only know the Type. I'm not sure how to convert the Type of the Weapon into the WeaponType enum value.
    – Holden
    Nov 21 at 4:21










  • 1) research Type class in MSDN. "Name" is in there somewhere. And enum too. Find out what ToString does. 2) I don't understand why/how you only have a Type object. How does the code know the Type in the first place? What does the UI communicate to the program?
    – radarbob
    Nov 21 at 4:38










  • I honestly probably did it wrong to start, but basically i'm trying to randomly generate everything I can. When I click "open chest" I randomly generate an Item there, and then determine if its a weapon, armor, boots, etc, and then what type of weapon. That's where I ended up with the Type so that I can see which type it is.
    – Holden
    Nov 21 at 4:47










  • but what is "Type"? Sure it represents a concrete something in your application domain. But as coded is it a C# Type class object or merely a string? You always capitalize it - is it a class you invented? "I have the Type" means what? There's an object of some class (what class?) that has a property called "type" that is a string set to the value "sword"? Some code might be helpful.
    – radarbob
    Nov 21 at 5:35












  • Just Generic Type. I haven't had to use it much prior to this so it's been a hill to climb over. I think I have figured that issue out, however now I"m running into the problem that when I return a newWeapon, the start method in Unity doesn't run unless it's in a gameObject so I need to figure out how to "initialize" that newWeapon upon creation.
    – Holden
    Nov 21 at 7:10













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%2f53403685%2finstantiating-gameobject-based-on-type%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














Create a weapons enum



public enum WeaponType {sword, crossbow, q36SpaceModulator }


Use that to tell your factory (@Draco18s comment) what to build - methods for creating weapons in this case.



public class WeaponFactory {
public Weapon Create (WeaponType thisWeapon) {
Weapon newWeapon;

switch (thisWeapon) {
case WeaponType.sword :
newWeapon = BuildSword();
break;

case WeaponType.crossbow:
newWeapon = BuildCrossbow();
break;

// and so on...
default: // we don't have whatever-you-asked-for
}

return newWeapon;
}

private Weapon BuildSword() { return new Sword(); }

}

// somewhere in code...
// you may have other parameters for particular weapon attributes
var sword = royalArmory.Create(Weapon.sword);





share|improve this answer





















  • I really like this concept, however at time of needing to create a weapon, I only know the Type. I'm not sure how to convert the Type of the Weapon into the WeaponType enum value.
    – Holden
    Nov 21 at 4:21










  • 1) research Type class in MSDN. "Name" is in there somewhere. And enum too. Find out what ToString does. 2) I don't understand why/how you only have a Type object. How does the code know the Type in the first place? What does the UI communicate to the program?
    – radarbob
    Nov 21 at 4:38










  • I honestly probably did it wrong to start, but basically i'm trying to randomly generate everything I can. When I click "open chest" I randomly generate an Item there, and then determine if its a weapon, armor, boots, etc, and then what type of weapon. That's where I ended up with the Type so that I can see which type it is.
    – Holden
    Nov 21 at 4:47










  • but what is "Type"? Sure it represents a concrete something in your application domain. But as coded is it a C# Type class object or merely a string? You always capitalize it - is it a class you invented? "I have the Type" means what? There's an object of some class (what class?) that has a property called "type" that is a string set to the value "sword"? Some code might be helpful.
    – radarbob
    Nov 21 at 5:35












  • Just Generic Type. I haven't had to use it much prior to this so it's been a hill to climb over. I think I have figured that issue out, however now I"m running into the problem that when I return a newWeapon, the start method in Unity doesn't run unless it's in a gameObject so I need to figure out how to "initialize" that newWeapon upon creation.
    – Holden
    Nov 21 at 7:10


















2














Create a weapons enum



public enum WeaponType {sword, crossbow, q36SpaceModulator }


Use that to tell your factory (@Draco18s comment) what to build - methods for creating weapons in this case.



public class WeaponFactory {
public Weapon Create (WeaponType thisWeapon) {
Weapon newWeapon;

switch (thisWeapon) {
case WeaponType.sword :
newWeapon = BuildSword();
break;

case WeaponType.crossbow:
newWeapon = BuildCrossbow();
break;

// and so on...
default: // we don't have whatever-you-asked-for
}

return newWeapon;
}

private Weapon BuildSword() { return new Sword(); }

}

// somewhere in code...
// you may have other parameters for particular weapon attributes
var sword = royalArmory.Create(Weapon.sword);





share|improve this answer





















  • I really like this concept, however at time of needing to create a weapon, I only know the Type. I'm not sure how to convert the Type of the Weapon into the WeaponType enum value.
    – Holden
    Nov 21 at 4:21










  • 1) research Type class in MSDN. "Name" is in there somewhere. And enum too. Find out what ToString does. 2) I don't understand why/how you only have a Type object. How does the code know the Type in the first place? What does the UI communicate to the program?
    – radarbob
    Nov 21 at 4:38










  • I honestly probably did it wrong to start, but basically i'm trying to randomly generate everything I can. When I click "open chest" I randomly generate an Item there, and then determine if its a weapon, armor, boots, etc, and then what type of weapon. That's where I ended up with the Type so that I can see which type it is.
    – Holden
    Nov 21 at 4:47










  • but what is "Type"? Sure it represents a concrete something in your application domain. But as coded is it a C# Type class object or merely a string? You always capitalize it - is it a class you invented? "I have the Type" means what? There's an object of some class (what class?) that has a property called "type" that is a string set to the value "sword"? Some code might be helpful.
    – radarbob
    Nov 21 at 5:35












  • Just Generic Type. I haven't had to use it much prior to this so it's been a hill to climb over. I think I have figured that issue out, however now I"m running into the problem that when I return a newWeapon, the start method in Unity doesn't run unless it's in a gameObject so I need to figure out how to "initialize" that newWeapon upon creation.
    – Holden
    Nov 21 at 7:10
















2












2








2






Create a weapons enum



public enum WeaponType {sword, crossbow, q36SpaceModulator }


Use that to tell your factory (@Draco18s comment) what to build - methods for creating weapons in this case.



public class WeaponFactory {
public Weapon Create (WeaponType thisWeapon) {
Weapon newWeapon;

switch (thisWeapon) {
case WeaponType.sword :
newWeapon = BuildSword();
break;

case WeaponType.crossbow:
newWeapon = BuildCrossbow();
break;

// and so on...
default: // we don't have whatever-you-asked-for
}

return newWeapon;
}

private Weapon BuildSword() { return new Sword(); }

}

// somewhere in code...
// you may have other parameters for particular weapon attributes
var sword = royalArmory.Create(Weapon.sword);





share|improve this answer












Create a weapons enum



public enum WeaponType {sword, crossbow, q36SpaceModulator }


Use that to tell your factory (@Draco18s comment) what to build - methods for creating weapons in this case.



public class WeaponFactory {
public Weapon Create (WeaponType thisWeapon) {
Weapon newWeapon;

switch (thisWeapon) {
case WeaponType.sword :
newWeapon = BuildSword();
break;

case WeaponType.crossbow:
newWeapon = BuildCrossbow();
break;

// and so on...
default: // we don't have whatever-you-asked-for
}

return newWeapon;
}

private Weapon BuildSword() { return new Sword(); }

}

// somewhere in code...
// you may have other parameters for particular weapon attributes
var sword = royalArmory.Create(Weapon.sword);






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 1:47









radarbob

3,83721624




3,83721624












  • I really like this concept, however at time of needing to create a weapon, I only know the Type. I'm not sure how to convert the Type of the Weapon into the WeaponType enum value.
    – Holden
    Nov 21 at 4:21










  • 1) research Type class in MSDN. "Name" is in there somewhere. And enum too. Find out what ToString does. 2) I don't understand why/how you only have a Type object. How does the code know the Type in the first place? What does the UI communicate to the program?
    – radarbob
    Nov 21 at 4:38










  • I honestly probably did it wrong to start, but basically i'm trying to randomly generate everything I can. When I click "open chest" I randomly generate an Item there, and then determine if its a weapon, armor, boots, etc, and then what type of weapon. That's where I ended up with the Type so that I can see which type it is.
    – Holden
    Nov 21 at 4:47










  • but what is "Type"? Sure it represents a concrete something in your application domain. But as coded is it a C# Type class object or merely a string? You always capitalize it - is it a class you invented? "I have the Type" means what? There's an object of some class (what class?) that has a property called "type" that is a string set to the value "sword"? Some code might be helpful.
    – radarbob
    Nov 21 at 5:35












  • Just Generic Type. I haven't had to use it much prior to this so it's been a hill to climb over. I think I have figured that issue out, however now I"m running into the problem that when I return a newWeapon, the start method in Unity doesn't run unless it's in a gameObject so I need to figure out how to "initialize" that newWeapon upon creation.
    – Holden
    Nov 21 at 7:10




















  • I really like this concept, however at time of needing to create a weapon, I only know the Type. I'm not sure how to convert the Type of the Weapon into the WeaponType enum value.
    – Holden
    Nov 21 at 4:21










  • 1) research Type class in MSDN. "Name" is in there somewhere. And enum too. Find out what ToString does. 2) I don't understand why/how you only have a Type object. How does the code know the Type in the first place? What does the UI communicate to the program?
    – radarbob
    Nov 21 at 4:38










  • I honestly probably did it wrong to start, but basically i'm trying to randomly generate everything I can. When I click "open chest" I randomly generate an Item there, and then determine if its a weapon, armor, boots, etc, and then what type of weapon. That's where I ended up with the Type so that I can see which type it is.
    – Holden
    Nov 21 at 4:47










  • but what is "Type"? Sure it represents a concrete something in your application domain. But as coded is it a C# Type class object or merely a string? You always capitalize it - is it a class you invented? "I have the Type" means what? There's an object of some class (what class?) that has a property called "type" that is a string set to the value "sword"? Some code might be helpful.
    – radarbob
    Nov 21 at 5:35












  • Just Generic Type. I haven't had to use it much prior to this so it's been a hill to climb over. I think I have figured that issue out, however now I"m running into the problem that when I return a newWeapon, the start method in Unity doesn't run unless it's in a gameObject so I need to figure out how to "initialize" that newWeapon upon creation.
    – Holden
    Nov 21 at 7:10


















I really like this concept, however at time of needing to create a weapon, I only know the Type. I'm not sure how to convert the Type of the Weapon into the WeaponType enum value.
– Holden
Nov 21 at 4:21




I really like this concept, however at time of needing to create a weapon, I only know the Type. I'm not sure how to convert the Type of the Weapon into the WeaponType enum value.
– Holden
Nov 21 at 4:21












1) research Type class in MSDN. "Name" is in there somewhere. And enum too. Find out what ToString does. 2) I don't understand why/how you only have a Type object. How does the code know the Type in the first place? What does the UI communicate to the program?
– radarbob
Nov 21 at 4:38




1) research Type class in MSDN. "Name" is in there somewhere. And enum too. Find out what ToString does. 2) I don't understand why/how you only have a Type object. How does the code know the Type in the first place? What does the UI communicate to the program?
– radarbob
Nov 21 at 4:38












I honestly probably did it wrong to start, but basically i'm trying to randomly generate everything I can. When I click "open chest" I randomly generate an Item there, and then determine if its a weapon, armor, boots, etc, and then what type of weapon. That's where I ended up with the Type so that I can see which type it is.
– Holden
Nov 21 at 4:47




I honestly probably did it wrong to start, but basically i'm trying to randomly generate everything I can. When I click "open chest" I randomly generate an Item there, and then determine if its a weapon, armor, boots, etc, and then what type of weapon. That's where I ended up with the Type so that I can see which type it is.
– Holden
Nov 21 at 4:47












but what is "Type"? Sure it represents a concrete something in your application domain. But as coded is it a C# Type class object or merely a string? You always capitalize it - is it a class you invented? "I have the Type" means what? There's an object of some class (what class?) that has a property called "type" that is a string set to the value "sword"? Some code might be helpful.
– radarbob
Nov 21 at 5:35






but what is "Type"? Sure it represents a concrete something in your application domain. But as coded is it a C# Type class object or merely a string? You always capitalize it - is it a class you invented? "I have the Type" means what? There's an object of some class (what class?) that has a property called "type" that is a string set to the value "sword"? Some code might be helpful.
– radarbob
Nov 21 at 5:35














Just Generic Type. I haven't had to use it much prior to this so it's been a hill to climb over. I think I have figured that issue out, however now I"m running into the problem that when I return a newWeapon, the start method in Unity doesn't run unless it's in a gameObject so I need to figure out how to "initialize" that newWeapon upon creation.
– Holden
Nov 21 at 7:10






Just Generic Type. I haven't had to use it much prior to this so it's been a hill to climb over. I think I have figured that issue out, however now I"m running into the problem that when I return a newWeapon, the start method in Unity doesn't run unless it's in a gameObject so I need to figure out how to "initialize" that newWeapon upon creation.
– Holden
Nov 21 at 7:10




















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53403685%2finstantiating-gameobject-based-on-type%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

Refactoring coordinates for Minecraft Pi buildings written in Python