• 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 Xaconi · Feb 11, 2013 at 10:36 PM · gameobjectaddcomponent

Add JS or C# object to GameObject?

Hi! i'm starting with Unity, and I have a problem. I have an array of objects (in JS or C#), and an array of cubes in the main stage. Well, I want to create a relation between these two parts. I can add the specific GameObject to each JS or C# in the array, but, in a specific case, I want to select with the mouse one cube in the screen. I can detect that cube with hit.collider, but, how i detect the specific JS or C# object? I've read a lot of stuff about AddComponent() and GetComponent() but I don't know if this is the answer. I need a method wich helps me to join the cube (GameObject) with the JS or C# object.

Here is the main:

 using UnityEngine;
 using System.Collections;
 
 // Main de la creació de l'escenari del joc.
 public class Main : MonoBehaviour {
 
     double moveSpeed = 1.0;
     double turnSpeed = 1.0;
     
     // Variables del tauler de joc.
     double ampladaPosicio = 2.0;
     double alturaPosicio = 0.5;
     double llargadaPosicio = 3.0;
     
     static int llargadaFitxesTauler = 10;
     static int ampladaFitxesTauler = 6;
     
     Fitxa [,] tauler = new Fitxa[llargadaFitxesTauler,ampladaFitxesTauler];
     Personatge p1;
     
     
     // Creació de tauler / personatges / cartes de l'usuari / bases.
     // Posicionament de la càmara.
     void Start () {
         float x = -6.302567f;
         float y = 14.22612f;
         float z = 13.3999f;
         Vector3 vector = new Vector3(x,y,z);
         transform.position = vector;
         
          for (int fitxesFila = 0; fitxesFila < llargadaFitxesTauler; fitxesFila++) {
             for (int fitxesColumna = 0; fitxesColumna < ampladaFitxesTauler; fitxesColumna++) {
                 GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
                 //cube.AddComponent(Rigidbody);
                 double posicioX = fitxesColumna * ampladaPosicio;
                 double posicioZ = fitxesFila * llargadaPosicio;
                 Vector3 v3 = new Vector3 ((float)posicioX, 0, (float)posicioZ);
                 cube.transform.position = v3;
                 v3 = new Vector3 ((float)ampladaPosicio, (float)alturaPosicio, (float)llargadaPosicio);
                 cube.transform.localScale = v3;
                 cube.collider.tag = "Fitxa";
                 tauler[fitxesFila, fitxesColumna] = new Fitxa(fitxesFila, fitxesColumna, (float)posicioX, (float)posicioZ, cube);
             // DOUBT HERE    cube.AddComponent(Fitxa);
             }
         }
         
        var figura = GameObject.CreatePrimitive(PrimitiveType.Cylinder);
        p1 = new Personatge(figura, tauler[0,0]);
     }
     
     void Update () {
         /*if(Input.GetButtonDown("Jump")){
             transform.position.z += 1.0;
         }else if(Input.GetButtonDown("left")){
             transform.position.z -= 1.0;
         }else if(Input.GetAxis("Horizontal") < 0){
             transform.position.x += 1.0;
         }else if(Input.GetAxis("Horizontal") > 0){
             transform.position.x -= 1.0;
         }*/
         
         //Vector3 v = new Vector3(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"), 0);
         //Debug.Log(Input.mousePosition.x);
         
         if ( Input.GetMouseButtonDown(0)){
             RaycastHit hit = new RaycastHit();
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             float marge = 100;
              if (Physics.Raycast (ray, out hit, marge)){
                 if(hit.collider.tag.Equals("Player")){
                     Debug.Log("Seleccionat");
 //                    GameObject objecte = hit.collider.gameObject;
 //                    Objecte other = objecte.GetComponent("Objecte");
 //                    other.Select();
                 }
                 else if(hit.collider.tag.Equals("Personatge")){
                     Debug.Log("Seleccionat Personatge");
                     p1.seleccionarPersonatge();
                 }
                 else if(hit.collider.tag.Equals("Fitxa")){
                     GameObject fitxa = hit.collider.gameObject;
                     int X = (int) fitxa.transform.position.x / 2;
                     int Z  = (int) fitxa.transform.position.z / 3;
                     p1.mourePersonatge(tauler[Z,X]);
                     GameObject objecte = hit.collider.gameObject;
         // DOUBT HERE                Fitxa f = (Fitxa) objecte.GetComponent("Fitxa");
                     int y = 1;
                 }
             }
         }
 //        
 //        
 //        if(Input.GetButton("Forward")){
 //            transform.position += transform.forward * moveSpeed * Time.deltaTime;
 //        }
 //        if(Input.GetButton("Backward")){
 //            transform.position += - transform.forward * moveSpeed * Time.deltaTime;
 //        }
 //        if(Input.GetButton("Left")){
 //            transform.eulerAngles.y += - turnSpeed * Time.deltaTime;
 //        }
 //        if(Input.GetButton("Right")){
 //            transform.eulerAngles.y += turnSpeed * Time.deltaTime;
 //        }
     }
 }

And here the class object I want to join with the GameObject:

 using UnityEngine;
 using System.Collections;
 
 public class Fitxa : MonoBehaviour {
     
     public int posicioXTauler;
     public int posicioYTauler;
     public float posicioXmon;
     public float posicioYmon;
     public GameObject figura;
 
     public Fitxa(){
     
     }
 
     public Fitxa(int xTauler, int yTauler, float xMon, float yMon, GameObject cube){
         posicioXTauler = xTauler;
         posicioYTauler = yTauler;
         posicioXmon = xMon;
         posicioYmon = yMon;
         figura = cube;
     }
 }

I will apreciate any help, because I feel like I'm missing really important concepts here. Thanks in advance!

PS: I say "JS or C# object" because I tried with both two languages with no success...

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 phfatmonkey · Feb 12, 2013 at 12:12 AM

It's important to know that all GameObjects in Unity are just made up of a series of different components, for example, Mesh components, Transform components, Audio components, or Javascript or C# script components. All of these components are added to a GameObject to add functionality to it.

To access the script component on a GameObject obj, try this:

 Fitxa knownClass = obj.GetComponent<Fitxa >();

will get you the Fixta component on the GameObject (a C# class)

Additionally, if you don't know the class type and you need to query the object first (say, for example, it could be any of ten possible scripts attached), you can use GetComponenets. [This thread][1] has an example that should help.

Good luck. [1]: http://forum.unity3d.com/threads/33575-Get-*ALL*-components-in-a-GameObject

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.

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

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

A node in a childnode? 1 Answer

Why does IsSleeping() keep returning false? (Billiard Logic) 0 Answers

Can't add component because class doesnt exist 1 Answer

Making a game object un-render or turn-invisible through trigger? 1 Answer

Queuing Gameobjects 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