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 Gomorrah · Oct 23, 2010 at 01:24 PM · variablereferencechangeaccesscs1061

Changing variable of another object (C#)

Can some one help me please. I can't get my head around this problem. I am new to Unity and and still learning C#. So I thought I start with a simple board game.

I have an two objects:

Object 'TilePre' has a script with a public class called 'Goban' and a public string variable called 'status'

I would like to tell Object 'Goban' to change the value of the 'status' variable in class 'A'.

Can some one help? I tried the following links but I can't get it to work.

  • Accessing Other Objects

  • Accessing Other Components

  • How can I access other scripts and their functions?

I set the code back as it was before I tried the above methods.


Goban

using UnityEngine; using System.Collections;

public class Goban : MonoBehaviour {

public GameObject TilePre; public int gobanSize = 6;

// Use this for initialization void Start () { int rows = gobanSize; int columns = gobanSize;

 string [,] StatusArray = new string [rows, columns];

 for ( int i = 0; i < rows; i++)
 {
     for ( int j = 0; j < columns; j++)
     {   
         Vector3 position = new Vector3 (i, 0, j);
         Instantiate (TilePre, position, Quaternion.identity);

         TilePre.name = "Tile_" + i + "_" + j;
         TilePre.status = "E";

     }
 }


}

}


TileScript

using UnityEngine; using System.Collections;

public class TileScript : MonoBehaviour { public GameObject BeadWhitePre; public string tileStatus;

 void OnMouseDown () {
     Vector3 position = new Vector3 (transform.position.x, (transform.position.y + transform.localScale.y / 2) + (BeadWhitePre.transform.localScale.y / 2), transform.position.z);
     Instantiate (BeadWhitePre, position, Quaternion.identity);

     TilePre.tileStatus = "W";
 }

I get the following ERROR: error CS1061: Type UnityEngine.GameObject' does not contain a definition fortileStatus' and no extension method tileStatus' of typeUnityEngine.GameObject' could be found (are you missing a using directive or an assembly reference?)

The code : TilePre.status = "E"; in the Goban class is the problem. How do I fix this?

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
Best Answer

Answer by Jesse Anders · Oct 23, 2010 at 01:44 PM

I see at least two problems right off. The first is that you're instantiating a new object from the TilePre game object, but then you're modifying the original game object (the one that the new game object was instantiated from). I'm assuming what you're wanting is to modify the newly created clone; to do that, cast the return value of Instantiate() to a GameObject, and that will give you a reference to the new clone.

The second problem is that GameObject does not have a field called tileStatus. When you write TilePre.tileStatus, since TilePre is of type GameObject, the compiler goes looking for a member in the GameObject class called tileStatus. There is no such member, thus the error.

It looks like what you're wanting is to access the tileStatus field of a TileScript component that's attached to the game object. To facilitate this, you can use the GetComponent() function to acquire a reference to the component in question.

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 Gomorrah · Oct 23, 2010 at 03:33 PM 0
Share

Thank you Jesse. That helped. I changed contents in the for loop in the Goban class to: Vector3 position = new Vector3 (i, 0, j); GameObject tileClone = Instantiate(TilePre, position, Quaternion.identity) as GameObject; tileClone.name = "Tile_" + i + "_" + j; tileClone.GetComponent ().tileStatus="E"; That fixed both of the problems you mentioned problem. I learnt a lot today :)

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

No one has followed this question yet.

Related Questions

Access and Change variables from other Scripts? 2 Answers

How to use a float value from coroutine 1 and use in coroutine 2? 1 Answer

How to change Target of camera from Standard Assets 1 Answer

How to access gameObject variable script 2 Answers

GUI.Style referencing, one script to another 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