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 /
avatar image
0
Question by zefrof · Dec 03, 2013 at 05:23 PM · c#getcomponentstatic

Transfering variable between gameojects and scripts

When one of my my mobs in the scene dies, I want it to tell a script on a completely different object to add one to the count of dead eneies. I've tried GetComponent and Static variable with little success. I'm coding in C#, thanks for any help.

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

2 Replies

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

Answer by clunk47 · Dec 03, 2013 at 05:43 PM

There are indeed many ways to do this. Here's an example. In this example project, I have 2 cubes in my scene somewhere in front of my camera. I have an empty gameObject in the scene named "Scripts", with "EnemyList" class attached. Each cube has "Enemy" component attached. Each "Enemy" is added to the list in "EnemyList". In the example, you click on each object that is tagged "Enemy" to kill it, remove it from the list, and add up dead count. Project is attached to make things more easily understandable if you have any issues.

DOWNLOAD EXAMPLE PROJECT

 //EnemyList.cs
 
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class EnemyList : MonoBehaviour 
 {
     [HideInInspector]
     public List<GameObject> enemies = new List<GameObject>();
     int dead = 0;
 
     void Update()
     {
         foreach(GameObject go in GameObject.FindObjectsOfType (typeof(GameObject)))
         {
             if(go.tag == "Enemy" && !enemies.Contains(go) && !go.GetComponent<Enemy>().dead)
                 enemies.Add (go);
             if(go.tag == "Enemy" && go.GetComponent<Enemy>().dead && enemies.Contains(go))
             {
                 enemies.Remove(go);
                 dead++;
             }
         }
 
         //TESTING PURPOSES
         RaycastHit hit;
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if(Physics.Raycast (ray, out hit) && Input.GetMouseButtonDown(0))
         {
             if(hit.collider.GetComponent<Enemy>() && !hit.collider.GetComponent<Enemy>().dead)
                 hit.collider.GetComponent<Enemy>().dead = true;
         }
         //END OF TEST
     }
 
     void OnGUI()
     {
         GUILayout.Label("Dead Enemies - " + dead.ToString ());
     }
 }





 //Enemy.cs
 
 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour 
 {
     [HideInInspector]
     public bool dead = false;
 }
 
 



enemykilltest.zip (148.8 kB)
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
avatar image
0

Answer by thaiscorpion · Dec 03, 2013 at 05:35 PM

There are lots of different ways to access variables from other objects scripts, you can check this link for some examples:

Unity Script, Accessing other scripts

The easiest way is to use the GetComponent method and insert the object in the inspector. If you have treid this already you might want to paste your code to see where the problem is.

Comment
Add comment · Show 1 · 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 amaceika · Dec 03, 2013 at 05:40 PM 0
Share

I was going to write the same thing. Since i cant see the code i am not sure how you are going about sending the variable. You may want to look at sending a message to the object ins$$anonymous$$d of accessing it directly through GetComponent.

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

19 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

Related Questions

GetComponent C# 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Where is the Mistake in my Coin System? 1 Answer

an object reference is required for the non static field error 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