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 hill43 · Oct 12, 2012 at 06:35 PM · gameobjecttransformpositiongrid

transform.position error

Hey there. I have a system set up that creates an array of objects that forms a grid. I'm trying to make the grid start on the position of the gameObject that it's attached to, but I'm having trouble referring to the gameObject. Here's my code.

 using UnityEngine;
 using System.Collections;
 
 public class GridCreator : MonoBehaviour
 {
     
     public int width = 10;
       public int height = 10;
     public Cell[,] cells;
     public GameObject unit;
     
     public class Cell
        {
         public Vector3 pos;
         public int sizeMod = 1;
         
         public Cell(int x, int y)
            {
             pos = new Vector3((x*sizeMod)+gameObject.transform.position.x,0,(y*sizeMod)+gameObject.transform.position.y);
            }
     }
         
 
     void Awake ()
        {
         cells = new Cell[width, height];
            for (int y = 0; y < height; ++y)
         {
                for (int x = 0; x < width; ++x)
                {
                    cells[x,y] = new Cell(x, y);
                 GameObject newunit = Instantiate(unit, cells[x,y].pos, Quaternion.identity) as GameObject;
             }
         }   
     }
 }


The grid works and instantiates Units correctly if I take the additives (+gameObject.transform.position.x/y) out of the equation here

    pos = new Vector3((x*sizeMod)+gameObject.transform.position.x,0,(y*sizeMod)+gameObject.transform.position.y);

But when I leave them in I get this error

Cannot access a nonstatic member of outer type `UnityEngine.Component' via nested type `GridCreator.Cell'

Am I trying to access the variable incorrectly? Not sure what I'm doing wrong. 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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Owen-Reynolds · Oct 12, 2012 at 07:46 PM

The rule causing the problem is that classes can only use things "inside" of them. `Cell` can't use globals `width` or `GameObject` like a function would be able to, since `Cell` isn't really part of `GridCreator` (some other script could make some cells, but have no gridCreator anywhere in the scene.)

Maybe change the constructor: `public Cell(gameObject myBase, float x, float y);` Might also move `sizeMod` up into Grid.

What some people do is have a back pointer, so Cell would have `GridCreator mygrid;`, set at creation, and could use `myGrid.gameObject`. But not worth it for just cells.

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 hill43 · Oct 12, 2012 at 09:22 PM 0
Share

Hey, thanks a lot! That really helps. I think a lot of my problems are caused by confusing classes and functions- I'm going to do some reading on that subject.

Anyways, everything made sense besides the 'back pointer' section- not really sure what you meant by that.

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

10 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

Related Questions

Take final position after a force applied to a GameObject 1 Answer

Locating Position of Object not Working? 2 Answers

Need insight on strange (simple) 2D object transform bug 0 Answers

Can i transform the position of an object to another object? 1 Answer

[CODE]Keep Gameobject position exactly at mouse cursor when cast float position to int 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