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 EpicCrownKing · Nov 17, 2019 at 07:03 AM · arrayslistscopy

Copying lists cross-class not working correctly

So, this is a bit of a big one, but I'm currently working for a hat/vanity system for my game. I want the player to be able to unlock and select the desired hat, currently using a large array of all hats and lists of unlocked hats. The scripts themselves are a bit lengthy, but I'll do my best to slim them down to what's necessary.

TL;DR at bottom.

The GameManagerScript's here:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 
 public class GameManagerScript : MonoBehaviour
 {
     private GameObject player;
     private GameObject vanityManager;
     public Hat[] hats;
     public List<Hat> ownedHats;
     public Sprite hat;
 
     void OnEnable() {SceneManager.sceneLoaded += OnSceneLoaded; }
     void OnDisable(){SceneManager.sceneLoaded -= OnSceneLoaded;}
 
     private bool newHat;
 
     void addHat(int hat)
     {
         ownedHats.Add(hats[hat]);
         newHat = true;
     }
 
     private void OnSceneLoaded(Scene scene, LoadSceneMode mode)
     {
         if (scene.buildIndex == 1)
         {
             vanityManager = GameObject.FindGameObjectWithTag("VanityManager");
 
             GameObject newHatText = GameObject.FindGameObjectWithTag("NewHatText");
 
             newHat = false;
             if (coins >= 5)
             {
                 addHat(3);
             }
             if (coins >= 10)
             {
                 addHat(4);
             }
             //...etc, for about 10 more or so
 
             if (newHat == true)
             {
                 newHatText.SetActive(true);
             }
 
             vanityManager.GetComponent<VanityManagerScript>().ownedHats = ownedHats; //THIS IS THE LINE THAT BREAKS THE CODE
         }
 
        
     }
   
 
     //DontDestroyOnLoad
     private static GameManagerScript gameManager;
     
     void Awake()
     {
         if (gameManager == null)
         {
             gameManager = this.gameObject.GetComponent<GameManagerScript>();
             GameObject.DontDestroyOnLoad(this);
         }
         else
         {
             Destroy(gameObject);
         }
     }
 }

And the VanityManagerScript's here:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class VanityManagerScript : MonoBehaviour
 {
     public List<Hat> ownedHats;
 
     public GameObject hat;
     public Text hatName;
 
     private GameObject gameManager;
 
     private int index = 1;
 
     void SetHat()
     {
         if(ownedHats[index] == null)
         {
             Color tempColor = hat.GetComponent<Image>().color;
             tempColor.a = 0f;
             hat.GetComponent<Image>().color = tempColor;
             gameManager.GetComponent<GameManagerScript>().hat = null;
             hatName.text = "None";
         }
         else
         {
             hat.GetComponent<RectTransform>().sizeDelta = new Vector2((ownedHats[index].hatSprite.bounds.size.x/2) * 100, 66);
             gameManager.GetComponent<GameManagerScript>().hat = ownedHats[index].hatSprite;
             hat.GetComponent<Image>().sprite = ownedHats[index].hatSprite;
             hatName.text = ownedHats[index].hatName;
             Color tempColor = hat.GetComponent<Image>().color;
             tempColor.a = 1f;
             hat.GetComponent<Image>().color = tempColor;
         }
     }
     public void increment()
     {
         if(index >= ownedHats.Count - 1)
         {
             index = 1;
         }
         else
         {
             index++;
         }
         SetHat();
     }
         
     public void decrement()
     {
         if (index <= 1)
         {
             index = ownedHats.Count - 1;
         }
         else
         {
             index--;
         }
         SetHat();
     }
         
     private void Start()
     {
         ownedHats = new List<Hat>();
         gameManager = GameObject.FindGameObjectWithTag("GameController");
     }
 }

Apologies for messy code, I didn't expect to be this alienated by errors...

know I'm working with lists wrong-previously I've stuck to arrays. I tried initializing lists in both places( listName = new List(); I believe ) and it's either produced more errors or stagnated.

TL;DR

The key problem is copying the lists between the two GameObjects without deleting the two. I've tried all solutions I could find on the forums. Just in case, I'm using 2019.3.0a11.

Thanks for your help, I know this is a big one.

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

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by prof · Nov 17, 2019 at 06:36 PM

Before jumping into unity, learn basic coding first. Youtube has plenty tutorals on both c# and general coding.

 public class ListHolder : MonoBehaviour{
     [SerializeField] private List<GameObject> originList;
 
     public List<GameObject> GetListReference(){
         return originList;
     }
 
     public List<GameObject> GetListCopy(){
         return new List<GameObject>(originList);
     }
 }



 public class ListReceiver : MonoBehaviour{
 
     public GameObject ListHolder;
     
     [SerializeField] private List<GameObject> listReference;
     [SerializeField] private List<GameObject> listCopy;
 
     private void Start(){
         listReference = ListHolder.GetComponent<ListHolder>().GetListReference();
         listCopy = ListHolder.GetComponent<ListHolder>().GetListCopy();
     }
 }

Lists in c# are refernce type. If you need a copy, create a new list

Comment
Add comment · Show 3 · 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 EpicCrownKing · Nov 17, 2019 at 08:55 PM 0
Share

This is helpful, I'll utilize it in my code. I've been program$$anonymous$$g for about four to five years now and never encountered lists, maybe once or twice in JS and Python; C# not at all. It's an unfamiliar topic to me. I learned all my program$$anonymous$$g from specific classes, so I've been trying to branch out, using more familiar things like Unity Learn and C# 7.0 In a Nutshell. I'd always used arrays up until now, but that's beside the point. I've been trying to $$anonymous$$ch myself what online and in-person classes couldn't.

avatar image prof EpicCrownKing · Nov 17, 2019 at 09:33 PM 0
Share

Here's a piece of advice then. Don't know what class does? Google it. Quick overview of c# by Derek Banas (including lists)

avatar image EpicCrownKing prof · Nov 19, 2019 at 06:07 AM 0
Share

Wow... kinda disappointed with myself... I was initializing the lists in Start and Awake, not upon the creation of the lists. All working now, the C# Survival Guide's been really helpful. Thanks for pointing me in the right direction.

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

118 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

Related Questions

Performance question - Loading large-ish amounts of text data in a mobile game? 1 Answer

Convert list of floats to float array 1 Answer

Using similar to GameObject.FindObjectsWithTag but for tag.contains. 0 Answers

List/Array should handle another List/Array - Upgrade System - Performance 1 Answer

Undo/back system using a List/Array 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