Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Genetix · Aug 03, 2021 at 10:18 PM · variablesclassstructassign-variableassignment

Unity value assignment help

I've been writing a new little project recently and I've gotten stuck on this bit. Instead of writing the newEntities stats as the stats on the list and leaving the values of the list alone, it updates the values of the list in real-time as the game is being played. Directly below is the only reference to the list I have within the project currently.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class World_Manager : MonoBehaviour
 {
     public List<Entity> activeEntities;
 
     void Start()
     {
         SpawnEntity("Player");
         SpawnEntity("Enemy");
         SpawnEntity("Enemy");
         SpawnEntity("Enemy");
         SpawnEntity("Enemy");
         SpawnEntity("Enemy");
     }
 
     public void SpawnEntity(string entityName)
     {
         List<Entity> entityList = GameObject.Find("Data Manager").GetComponent<EntityList>().entityList;
         for (int i = 0; i < entityList.Count; i++)
         {
             if (entityName == entityList[i].entityName)
             {
                 GameObject newEntity = Instantiate(entityList[i].entityObject, new Vector2(Random.Range(-10, 10), Random.Range(-4, 4)), Quaternion.identity);
                 newEntity.GetComponent<Statistics>().setStats(entityList[i]);
                 AddActiveEntity(newEntity.GetComponent<Statistics>());
                 return;
             }
         }
     }
 
     public void AddActiveEntity(Statistics newStats)
     {
         newStats.activeID = activeEntities.Count;
         activeEntities.Add(newStats.entityStats);
     }

Here is the List itself

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEditor;
 
 public class EntityList : MonoBehaviour
 {
     public List<Entity> entityList = new List<Entity>();
 
     void Awake()
     {
         entityList.Add(new Entity("Player", 0, new Vector2(0, 0), (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Entities/Player.Prefab", typeof(GameObject)), 100, 100));
         entityList.Add(new Entity("Enemy", 1, new Vector2(0, 0), (GameObject)AssetDatabase.LoadAssetAtPath("Assets/Prefabs/Entities/Enemy.Prefab", typeof(GameObject)), 100, 100));
     }
 }

and here is the Entity class I use to define the lists type

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 [System.Serializable]
 public class Entity
 {
     public string entityName;
     public int entityListID;
     public Vector2 entityPos;
     public GameObject entityObject;
 
     public float entityHealth;
     public float entityMana;
 
     public Entity(string entityNme, int listID, Vector2 objPos, GameObject entityObj, float health, float mana)
     {
         entityName = entityNme;
         entityListID = listID;
         entityPos = objPos;
         entityObject = entityObj;
         entityHealth = health;
         entityMana = mana;
     }
 }

Finally, here is the players stats page, which is currently what tells each instantiated gameObject what its stats are

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Statistics : MonoBehaviour
 {
     public int activeID;
     public Entity entityStats;
 
     void Update()
     {
         entityStats.entityPos = gameObject.transform.position;
     }
 
     public void setStats(Entity newEntityStats)
     {
         entityStats = newEntityStats;
         entityStats.entityHealth = newEntityStats.entityHealth * Random.Range(0.4f, 1.7f);
     }
 }
 

If anyone can help me out or needs more information let me know. Much appreciated.

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
0

Answer by Genetix · Aug 04, 2021 at 03:41 AM

Worked through it throughout the night and figured out a way through it.

 public void SpawnEntity(string entityName)
     {
         Entity entity = GameObject.Find("Data Manager").GetComponent<EntityList>().GetEntity(entityName);
         GameObject newEntity = Instantiate(entity.entityObject, new Vector2(Random.Range(-10, 10), Random.Range(-4, 4)), Quaternion.identity);
         newEntity.GetComponent<Statistics>().entityStats = new Entity(
             entity.entityName, 
             entity.entityListID, 
             entity.entityObject, 
             entity.entityExperience, 
             entity.entityHealth, 
             entity.entityMana, 
             entity.entityStrength, 
             entity.entityConstitution, 
             entity.entityDexterity, 
             entity.entityIntelligence, 
             entity.bonusHealth, 
             entity.bonusSpellDamage);
         AddActiveEntity(newEntity.GetComponent<Statistics>());
     }

Eventually came up with this. It seems assigning the type as a whole didn't work but assigning each individual value to the new instantiated object works.

I'll be honest I found this out by just messing with the code in various different ways until I eventually got to this so I'm not sure the reason why the original method didn't work.

If someone could explain it to me I would appreciate it, thanks.

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

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

128 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

Related Questions

I want to assign multiple variables without having to type the class on the side every time. 1 Answer

Problems with editing a class from the editor 1 Answer

Load txt file into a class 0 Answers

My structs are acting like classes in scriptable object. 1 Answer

Can I choose what to pass into .getcomponent<{VARIABLE}>(); 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