Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Sep 29, 2013 at 09:23 AM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by Afropenguinn · Sep 29, 2013 at 03:19 AM · objectarraysvariables

Getting a variable from an object

I have several objects that run on the same script. The idea is that they both count down and then attack each other (though for now they just attack whatever monster is second in the array). using UnityEngine; using System.Collections;

 public class MonsterAI : MonoBehaviour
 {
     public string name = "NoName";
     public float hp = 100f;
     public float attack = 10f;
     public float speed = 10f;
     public float team = 1f;
     
     float action_bar = 100f;
     public GameObject[] target;
     
     void Start()
     {
     
     }
     
 
     void Update()
     {
         //ticking down their action bar
         if (action_bar > 0f)
         {
             action_bar -= speed * Time.deltaTime;
         }
         else
         {
             target = GameObject.FindGameObjectsWithTag("Monster");
             Attack (target[1]);
             Debug.Log (name + " has ended their turn.");
             action_bar = 100f;
         }
     }
     
     //functions
     void Attack(GameObject target)
     {
         Debug.Log (name + " attacks " + target.name + ".");
     }
 }

The problem is in Debug.Log (name + " attacks " + target.name + "."); the second name it gives is that of the object, not the variable within the object. One has the variable named "Steve" and the other "Bob" but both object's names are "Monster", so it says "Steve attacks Monster." when I would like it to say "Steve attacks Bob.". How do you get a variable from an object passed into a function like that?

Comment
Add comment · Show 2
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 tw1st3d · Sep 29, 2013 at 04:27 AM 0
Share

Create a function that returns the string, like,

 Player.getName();
 
 function getName()
 {
     return this.name;
 }
avatar image Afropenguinn · Sep 29, 2013 at 04:39 AM 0
Share

So there is no way to pull a public variable from an object without a function within the other object?

1 Reply

  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Sep 29, 2013 at 04:45 AM

Scripts are components, so you will need to use GetComponent() to get access to the MonsterAI script and the 'name' in that script:

 var monsterName = target.GetComponent(MonsterAI).name;

Then your output:

 Debug.Log (name + " attacks " + monsterName + ".");

Here are links concerning accessing other game objects and components:

http://docs-jp.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

http://docs-jp.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Components.html

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

Follow this Question

Answers Answers and Comments

16 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

Related Questions

Array empties values on RunTime? 1 Answer

Convert Object to a Float 1 Answer

Creating Object Arrays 2 Answers

Float seen as Object, help? 1 Answer

how can I make a box with flat cubes(which I want to call a 'tile')? and have each tile detect collision? 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