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 /
  • Help Room /
avatar image
0
Question by CaioPeres · Dec 17, 2019 at 07:08 PM · gameobjectobjectfunctionvaluesenemy spawn

How to Access Values from a Object,How to access values from a object

Hi. I have a issue to access values from a object attached a prefab.

To better understanding i will put here some scripts:

 //SpawnerManager.cs
 using UnityEngine;
 public class SpawnerManager : MonoBehaviour
 {
         public Transform spawnPoint;
 
         public void SpawnEnemy(GameObject Enemy){
             Instantiate(Enemy.transform, spawnPoint.position, spawnPoint.rotation);
         }
 
 }

Human.cs is the Object. I have one called Spider with the same attributes but different Values.

 //Human.cs
     using UnityEngine;
     public class Human : MonoBehaviour {
             public static GameObject Prefab;
             static int hp = 100, goldValue = 1;
             static float speed = 10f;
             Enemy Inimigo = new Enemy(hp, goldValue, speed, Prefab);
     }

Enemy.cs is the class for the objects Human and Spider: using UnityEngine;

       public class Enemy
 {
 
        int hp, goldValue;
        float speed;
 
         GameObject teste;
 
  
 
        public Enemy(int _hp, int _goldValue, float _speed, GameObject _teste)
 
        {
 
              //this is a constructor for the class. When a new Vehicle is created,
 
              //you can use this constructor to execute the following code
 
              hp = _hp;
 
              goldValue = _goldValue;
 
              speed = _speed;
 
              teste = _teste;
 
        }
 
 }



So, i have two buttons in a panel, these buttons when clicked they call the function SpawnEnemy() from SpawnerManager. The Enemy is Spawning because i attached the Prefab Human / Spider. In the Human Prefab i have attached the Human.cs and Spider Prefab, Spider.cs.

So my question is, How can i get the value from the Object Human.cs or Spider.cs in the SpawnEnemy()?

,Hi. I have a problem to access values from a object.

I have a SpawnerManager :

 using UnityEngine;
 
 public class SpawnerManager : MonoBehaviour
 {
         public Transform spawnPoint;
 
         public void SpawnEnemy(GameObject Enemy){
             Instantiate(Enemy.transform, spawnPoint.position, spawnPoint.rotation);
         }
 
 }


And i have a Panel with 2 buttons calling the function SpawnEnemy from the SpawnerManager. 1 should spawn a Human Enemy and another button should spawn spider enemy. But this is not my issue, each Enemy Prefab (Human and Spider) is a object like :

 using UnityEngine;
 
 public class Human : MonoBehaviour {
         public static GameObject Prefab;
         static int hp = 100, goldValue = 1;
         static float speed = 10f;
         Enemy Inimigo = new Enemy(hp, goldValue, speed, Prefab);
 }

and the Enemy which is the type:

 using UnityEngine;
 
       public class Enemy
 {
 
        int hp, goldValue;
        float speed;
 
         GameObject teste;
 
  
 
        public Enemy(int _hp, int _goldValue, float _speed, GameObject _teste)
 
        {
 
              //this is a constructor for the class. When a new Vehicle is created,
 
              //you can use this constructor to execute the following code
 
              hp = _hp;
 
              goldValue = _goldValue;
 
              speed = _speed;
 
              teste = _teste;
 
        }
 
 }

Okay. After all, to resume my question: How can i get the values from the objects like (hp, goldValue ...) in the SpawnerManager?

Thank you.

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.

Follow this Question

Answers Answers and Comments

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

Problem with int values + 1 gives unexpected values 1 Answer

How do I prevent the destroying of all instances of an object in Unity? 0 Answers

Object to GameObject casting error c# (from object in scene not a prefab) 0 Answers

Temporary Gameobject Reference in Function without Instantiating 2 Answers

How do I pass in a GameObject as a parameter when I'm instantiating a class? 0 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