• 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 /
  • Help Room /
avatar image
0
Question by kobaini94 · May 17 at 07:52 AM · arraysitemselementclass object

Referencing multiple class array variable element in script

Hello everyone (and sorry if this is complicated/confusing, it's my first question on this site)! I'm trying to make a simple text-based adventure demo for my game design portfolio and I'm an amateur at best when it comes to coding. I'm using some old code from an online course and basically what I'm trying to do is have multiple item interactions in different rooms or target items while using the same item for usage. For this I tried having in the Item script, a class array variable named Interactions with at least two elements. Like this: alt text

However, I tried making sense of the current code in order to make a specific array element run depending on the designed location, but to no avail. This are the classes that I used for item interaction:

Interaction.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class Interaction
 {
     public Action action;
 
     [TextArea]
     public string response;
 
     public List<Item>itemsToDisable = new List<Item>();
     public List<Item>itemsToEnable = new List<Item>();
     
    
 
     // public List<Item> connectionsToDisable = new List<Item>(); 
     // public List<Item> connectionsToEnable = new List<Item>();
     // not in current design to have item interaction that enables/disables location connectors
 }



Item.cs

 public class Item : MonoBehaviour
 {
 
     public string itemName;
     [TextArea]
     public string description;
     public bool playerCanTake;
     public bool itemEnabled = true;
     public Interaction[] interactions;
     public Item targetItem = null;
 
 
 
 
     public bool InteractWith(GameController controller, string actionKeyword)
     {
     
         foreach (Interaction interaction in interactions)
         {
                // if bool statement on locations to execute a specific interaction element
            
             if (interaction.action.keyword == actionKeyword)
             {
                 foreach(Item disableItem in interaction.itemsToDisable)
                     disableItem.itemEnabled = false;
 
                 foreach (Item enableItem in interaction.itemsToEnable)
                     enableItem.itemEnabled = true;
 
                 controller.currentText.text = interaction.response;
 
                 return true;
             }
         }
         return false;
     }
 }

Use.cs

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [CreateAssetMenu(menuName = "Actions/Use")]
 public class Use : Action
 {
     public override void RespondToInput(GameController controller, string noun)
     {
         //use items in room
         if (UseItems(controller, controller.player.currentLocation.items, noun))
             return;
         //use item in inventory
         if (UseItems(controller, controller.player.inventory, noun))
             return;
 
         controller.currentText.text = "I couldn't do that.";
     }
 
     private bool UseItems(GameController controller, List<Item> items, string noun)
     {
         foreach (Item item in items)
         {
             if (item.itemName == noun)
             {
                 if (controller.player.CanUseItem(controller, item))
                 { if (item.InteractWith(controller, "use"))
                         return true;
                 }
                 controller.currentText.text = "It didn't do anything.";
 
                 return true;
             }
         }
         return false;
     }
 }




Player.cs

 using System;
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Player : MonoBehaviour
 {
     public Location currentLocation;
     public List<Item> inventory = new List<Item>();
     public Interaction interaction;
 
 
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         
     }
 
     public bool ChangeLocation(GameController controller, string connectionNoun)
     {
         Connection connection = currentLocation.GetConnection(connectionNoun);
         if (connection != null)
         {
             if (connection.connectionEnabled)
             {
                 currentLocation = connection.location;
                 return true;
             }
         }
         return false;
     }
 
     internal bool CanUseItem(GameController controller, Item item)
     {
         if (item.targetItem == null)
             return true; 
         
 
         if (HasItem(item))
             return true;
 
         if (currentLocation.HasItem(item))
             return true;
 
         return false;
     }
 
     private bool HasItem(Item itemToCheck)
     {
         foreach (Item item in inventory)
         { 
           if (item == itemToCheck)
                 return true;
         }
         return false;
     }
 }

Does anyone know a solution for making this possible? If yes, much appreciated!

untitled.jpg (89.1 kB)
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

183 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image 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

Getting the element that has the same element number on another list ? 2 Answers

Array of Arrays 3 Answers

Array cannot read Greek characters 1 Answer

Randomize text position for 2D Quiz C# 0 Answers

Getting the closest GameObject to the player from an array 1 Answer


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