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 MancVandaL · Jul 26, 2017 at 03:22 PM · gameobjectarrayscript.

How do I access an object script variable that is in a 2D array?

Hi. I have a 2D array that adds a class I created that has a gameobject and various details about that GO in it.

What I want to do is attach a script to each GO in the array and be able to access that script and set a bool in there. However, I'm having problems doing that.

This is the class I created for the 'tiles' (it's a Match 3 game I'm making). Each tile has the GO and other info as you can see. I can access the GO tranform etc OK, so I can move them about, but not the script I have on them.

 public class Tile 
 {
     public GameObject tileObj;
     public string tag;
     public bool isHMatched;
     public bool isVMatched;
     public bool isMovingDown;
     
 
     public Tile(GameObject obj, string n, bool hMatched, bool vMatched, bool movingDown)
     {
         isHMatched = hMatched;
         isVMatched = vMatched;
         isMovingDown = movingDown;
         tileObj = obj;
         tag = n;
     }
 
 }

I iterate through the array with for loops and create the tiles like this

         int blockToCreate = Random.Range(0, candyBlock.Length); // Pick a random block to spawn.

         GameObject blockCandy = Instantiate(candyBlock[blockToCreate], new Vector3(boardPosition.transform.position.x + blockSize *  c, boardPosition.transform.position.y + blockSize * r, boardPosition.transform.position.z), Quaternion.identity); //Spawn the block

         blockCandy.transform.parent = boardPosition.transform; // Set the parent of the block to the boardposition gameobject.

         tileArray[c, r] = new Tile(blockCandy, blockCandy.tag, false, false,false); // Add the created block to the array.  

I then used this code to try and access the variable in the GO script.

 tileArray[c, r].tileObj.gameObject.GetComponent<Tile>().isMovingDown = true;

As you can see, this allows me access to the isMovingDown bool in the script on the object, just fine. But when I run the code I get this error.

 ArgumentException: GetComponent requires that the requested component 'Tile' derives from MonoBehaviour or Component or is an interface.

So I thought, OK I'll derive from Monobehaviour in the class I created by adding : Monobehaviour to the class.

 public class Tile : Monobehavior
 {
  ...

Now when I run, I get the error

 You are trying to create a MonoBehaviour using the 'new' keyword.  This is not allowed.  MonoBehaviours can only be added using AddComponent().  Alternatively, your script can inherit from ScriptableObject or no base class at all

I've asked around and was told that I should use AddComponant<> to add the script so it can be used. However, I can't find how to use this properly. I've tried this.

 tileScript = tileArray[c, r].tileObj.gameObject.AddComponent<BlockScript>();

Then using tilescript. and trying to get access to the variable on the object script but it doesn't work. The autocomplete gives me no option to do that. I suspect (I know) I'm doing this all wrong, so can anyone help me out with this?

I know this was a long post, but I try to give as much information as I can when asking for help. No point skimming over things.

Thanks very much 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

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

139 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

Related Questions

One script with multiple Gameobject only one Works 0 Answers

Getting collision for individual gameobjects in an array and changing properties of other gameobjects from that array 0 Answers

Access GameObject Array and Turn Render on via the key pressed - Keys are already named based on game object. 0 Answers

Instantiated bullet wont destroy 1 Answer

How to check if there are no more objects in array (with conditions) 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