• 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 /
This question was closed May 31, 2018 at 12:15 PM by jukka-tamminen for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by jukka-tamminen · May 31, 2018 at 11:56 AM · classscriptableobjectinventoryinventory systemitems

Inventory armor wielding proplem,How to convert from derived to base

Hi, i have tried to do armor swapping script for my projet and got stuck with classes. any idea how to make funktion where you can input armor iten thats class is "Armor" an asiing it to variable with type "ArmorTorso". "Armor" is scriptable object and "ArmorTorso" is derived from it. Here is my code and proplems in it:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Inventory : MonoBehaviour {
 
 
 
     public List<Item> inventory;
 
     private List<Armor> tempWieldetArmor;
     public List<Armor> wieldetArmor
     {
 
         get { tempWieldetArmor = new List<Armor>();
 
             if (armorChest != null)
                 tempWieldetArmor.Add(armorChest);
             if (armorHead != null)
                 tempWieldetArmor.Add(armorHead);
             if (armorLegs != null)
                 tempWieldetArmor.Add(armorLegs);
             if (armorArms != null)
                 tempWieldetArmor.Add(armorArms);
 
             return tempWieldetArmor;
 
         }
 
     }
     public TorsoArmor armorChest;
     public HeadArmor armorHead;
     public LegArmor armorLegs;
     public ArmArmor armorArms;
 
     public Weapon weapon;
     public Shield shield;
     
     public int inventorySize = 20;
 
     
     public bool WieldFromInventory( Armor armorToWield)    // problem 1. how to refer armor correctly
     {
         Armor tempArmor = new Armor();
 
         if (armorToWield == null)
             return false;
 
         if(armorToWield is TorsoArmor)
         {
             if (armorChest == null)
             {
                 armorChest = armorToWield;                  // proplem 2, how to armorChest to armorToWield (convert type "armor" to "torsoarmor")
             }
             else
             {
                 tempArmor = armorChest;
                 armorChest = armorToWield;                  // proplem 2, how to armorChest to armorToWield 
                 RemoveFromInventory(armorToWield);
                 AddToInventory(tempArmor);
                 tempArmor = new Armor();
             }
 
 
         }
 
         if (armorToWield is HeadArmor)
         {
             // chacge somne armor
         }
 
         if (armorToWield is LegArmor)
         {
             // chacge somne armor
         }
 
         if (armorToWield is ArmArmor)
         {
             // chacge somne armor
         }
 
 
     }
     public bool AddToInventory(Item item)
     {
         if (inventory.Count >= inventorySize)
             return false;
         else
         {
             inventory.Add(item);
             return true;
         }
 
     }
 
     public bool RemoveFromInventory(Item item)
     {
         if (!inventory.Contains(item))
             return false;
 
         else
         {
             inventory.Remove(item);
             return true;
         }
         
     }
 
 
 }
 
 Blockquote





Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0

Answer by jukka-tamminen · May 31, 2018 at 12:15 PM

problem solved, i just had to add (TorsoArmor) "to line 68 and 77" in front of "armorToWield"

Comment
Add comment · 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

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

88 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

Related Questions

Inventory with Upgradable Items and Varying Stats 2 Answers

Scriptableobject List and Instantiating objects from it 3 Answers

How i can make a script-made button interactable? 0 Answers

Inventory and scriptable object items - please point me in the right direction 1 Answer

'Item' class with various effects 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges