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 Levithan6785 · Jul 14, 2014 at 01:29 AM · variablesaccessing

Accessing Other Script Variables (C#)

I am having an issue trying to access another script's variables from another script. Both of these scripts I am referencing to are on separate gameobjects. Script I am trying to access is named "InventoryS2" and the gameobject is named "ThePlayer". The second script is called "MobT" and the gameobject it is on is called "Cube".

InventoryS2(Only Showed the relevent code, rest is GUI related things.)

 using UnityEngine;
 using System.Collections;
 
 public class InventoryS2 : MonoBehaviour 
 {
     private int Menu,Options,Inventory,Map,Skills,Stats = 0;
     public Vector2 scrollPosition = Vector2.zero;
     public float GoldCount = 0;

MobT

 using UnityEngine;
 using System.Collections;
 
 public class MobT : MonoBehaviour 
 {
     public float GoldDrop;
     public InventoryS2 Inventory;
 
     void Awake()
     {
         Inventory = GameObject.Find("ThePlayer").GetComponent ("InventoryS2");
     }
 
     void OnMouseDown()
     {
         GoldDrop = Random.Range (0,10);
         Inventory.GoldCount = Inventory.GoldCount + GoldDrop;
         Destroy (gameObject);
         Debug.Log (GoldDrop);
     }
 }

MobT is designed as a purpose of testing for when I click on the gameobject it will be destroyed and the GoldCount of the InventoryS2 script will increase at random between 1 and ten.

I would like responses that don't tell me to use static variables.

Comment
Add comment · Show 1
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 oddgoo · Jul 14, 2014 at 04:58 AM 0
Share

What is the error you are getting?

2 Replies

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

Answer by robertbu · Jul 14, 2014 at 07:08 AM

The problem is that you are using the string version of GetComponent(). When you pass a string to GetComponent(), the type of object you get back is 'Component'...the base class for all components. This happens because the compiler does not know the type. And you cannot assign a Component class to an InventoryS2 variable without casting (which you are not doing here).

My recommended fix is use the generic version of GetComponent(). Line 11 would then be:

 Inventory = GameObject.Find("ThePlayer").GetComponent<InventoryS2>();

 
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 Levithan6785 · Jul 14, 2014 at 10:15 AM 0
Share

Thanks, this fixed my problem and will solve what I am trying to do.

avatar image
0

Answer by uanmanarmy · Jul 14, 2014 at 10:27 AM

What I actually do is Creating 2 public variables.

For example

 public NameOfTheReferanceScript objectScript;
 public GameObject object;

I am making an Empty GameObject and I assign to it all my screept's that are not really visible.

Than Im assigning this empty object to those 2 public variables from Above.

in

 void Start()
 {
  objectScript = object.GetComponent <NameOfTheReferanceScript> ();
 }

Than with this new created object "ObjectScript" i can get all what I wont from NameOfTheReferanceScript scrip.

And you can get it by - objectScript. .... whatyou want.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Accessing other gameobject's script variables : why doesn't this work? 2 Answers

Adding variables from all scripts 2 Answers

Script access (without script name) 1 Answer

Changing static variables from another script? 1 Answer

Easily cleaning up \ deleting unused variables? 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