• 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 AmazingB · Jan 04, 2014 at 11:56 PM · gameobjectaccessing

accesing a unknown GameObject from a script

I want to be able to see the statts of the Tile I am standing on througth scribt, but I dont know how to do it.

This is my script :

 var startPoint : Vector3;  
 var endPoint : Vector3;  
 var speed : float;  
 private var increment : float;  
 var isMoving : boolean;  
 var tileSpace : float;  
 var tileOn : String;  
 var tile : GameObject [];  
 var tileStatts : Tile;  
 var walkedDir : Vector3;  
 
 
 
 
 
 
 
 
 
 
 
 function Start () {
 
     startPoint =  transform.position;
     endPoint = transform.position; 
 
 
 }
 
 function Update () {
     
     
     var pos = transform.position;
     
     calculateTileOn ();
     
     
     if(tileOn == "Tree" || tileOn == "Stone" ){
         increment = 0;
         isMoving = true;
             switch (walkedDir){
             case Vector3.up :
             startPoint = transform.position;
             endPoint = new Vector3(transform.position.x, transform.position.y - tileSpace, transform.position.z );
             break;
             case Vector3.down :
             startPoint = transform.position;
             endPoint = new Vector3(transform.position.x, transform.position.y + tileSpace, transform.position.z );
             break;
             case Vector3.left :
             startPoint = transform.position;
             endPoint = new Vector3(transform.position.x - tileSpace, transform.position.y, transform.position.z );
             break;
             case Vector3.rigth :
             startPoint = transform.position;
             endPoint = new Vector3(transform.position.x + tileSpace, transform.position.y - tileSpace, transform.position.z );
             break;
             
         }
             
     }
     
     
     
     
     if(increment <=1 && isMoving == true){
         increment += speed/100;
         }
     else{
         isMoving = false;
     
     }
     if(isMoving){
         transform.position = Vector3.Lerp(startPoint, endPoint, increment);
         
     }
     if(Input.GetButton("forward") && isMoving == false){
         increment = 0;
         isMoving = true;
         startPoint = transform.position;
         endPoint = new Vector3(transform.position.x, transform.position.y + tileSpace, transform.position.z );
         walkedDir = Vector3.up;
         
     }    
     
     if(Input.GetButton("backward") && isMoving == false){
         increment = 0;
         isMoving = true;
         startPoint = transform.position;
         endPoint = new Vector3(transform.position.x, transform.position.y - tileSpace, transform.position.z );
         walkedDir = Vector3.down;
     }    
     
     if(Input.GetButton("left") && isMoving == false){
         increment = 0;
         isMoving = true;
         startPoint = transform.position;
         endPoint = new Vector3(transform.position.x - tileSpace, transform.position.y, transform.position.z );
         walkedDir = Vector3.left;
     }    
     
     if(Input.GetButton("rigth") && isMoving == false){
         increment = 0;
         isMoving = true;
         startPoint = transform.position;
         endPoint = new Vector3(transform.position.x + tileSpace, transform.position.y, transform.position.z );
         walkedDir = Vector3.rigth;
     }    
     
 
 
 
 
 }    
 
 
 
 
 
 function calculateTileOn() {
     
     
      var hit: RaycastHit2D = Physics2D.Raycast(transform.position, -Vector2.up);
         
         if(1 == 1){
             tileOn = hit.collider.gameObject.tag;
             tile =  GameObject.FindGameObjectsWithTag (tileOn);
             tileStatts = (tile).Tile;
             
             
             
         }
         
         
         
             
 }

I am cheking this in my calculateTile function.

Comment
Add comment · Show 2
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 AmazingB · Jan 05, 2014 at 12:56 AM 0
Share

thank you.

avatar image Benproductions1 · Jan 05, 2014 at 01:30 AM 0
Share

Please format your code, if you don't know how, watch the tutorial video on the right!

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Polantaris · Jan 05, 2014 at 12:52 AM

Overall you should probably use one base tile class, that all tiles will have or derive from. That way, when you investigate a tile, you can just grab the base tile class in a GetComponent<>() call. If that call fails (it won't return something, so you can test it in an if()), then you don't care about whatever your raycast hit, but if it does then you can go from there. You can try multiple if() statements to see if it's a child class based on base tile, if you need to, however depending on how in depth you go with your base tile class you may not need to in most situations.

To determine if your tile is a tree, stone, or whatever you want, you should just make an enum TileType, that has those things, and then you can set the TileType for each prefab of tile. That way, everything is under one parent script that you can use to do whatever you need to do.

EX: public enum TileType {Tree, Stone, Water, Path}; //etc.

Then you can reference it with TileType.Tree (or whatever you need).

Comment
Add comment · Show 4 · 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 AmazingB · Jan 06, 2014 at 12:31 AM 0
Share

I dont realy get it

avatar image Polantaris · Jan 06, 2014 at 01:04 AM 0
Share

You need to be more specific. I can't really give you any further help without knowing what you don't understand.

avatar image AmazingB · Jan 06, 2014 at 01:43 AM 0
Share

the getComponent the ifs and the enum

avatar image Polantaris · Jan 06, 2014 at 02:26 AM 1
Share

http://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html

You can get a GameObject's scripts by using GetComponent. When you do a Raycast, it returns a RaycastHit, which contains a collider which contains a gameObject. First, you must ensure that the Raycast actually hit something, by ensuring that there's a collider inside the RaycastHit, and that collider has a gameObject attached to it. Once you do that, you can reference that gameObject and call GetComponent, using the name of the Tile Script as your parameter. For UnityScript, it's something like this: hit.collider.gameObject.GetComponent(Tile);

This will return the reference to a Tile Object, or nothing at all. If the gameObject does not have a Tile script attached to it, it will return nothing. You do:

if(hit.collider.gameObject.GetComponent(Tile)) {//other code here}

If there's a Tile Object attached to that gameObject, then your code will continue. If not, it returns null (nothing), which makes the if statement evaluate to false, and the code inside the if block is skipped.

So you can do something like: Tile t = hit.collider.gameObject.GetComponent(Tile);

Once you've done that, you have access to all public members of your Tile class, just like you do for any other object. You can then use the created TileType enum to deter$$anonymous$$e what type of Tile you're dealing with.
For example, TileType type = t.tileType;

From there, you can do an if statement to deter$$anonymous$$e what kind of tile you are dealing with and work with it accordingly. Look up enums here: http://en.wikipedia.org/wiki/Enumerated_type

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Using script's method from all of the gameobjects that has that script 2 Answers

How to create 3D game object in specified pixel size? 2 Answers

Unity3D Pressure Plate request. 3 Answers

Trying to delay movement of a GameObject 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