• 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
Question by RobotReebot · Oct 17, 2014 at 02:50 PM · c#instantiategetcomponent

GetComponent don't work (i am desperate)

Hi guys, i need you help.

I am working on a script that should assign "OggettoDaMuovere" (Move an Object) when it is instantieted. This is the code:

 using UnityEngine;
 using System.Collections;
 
 public class RetroGameHandler : MonoBehaviour {
     public GameObject ret;
 
     public GameObject tri;
     public GameObject tri90;
     public GameObject maincamera;
     float time2= 1.1f;
     float time;
     int rnd;
     int contaOggetti= 1;
     float conta= 0f;
     GameObject[] ListaOggetti= new GameObject[100];
     // Use this for initialization
     void Start () {
         time = 5;
     }
     
     // Update is called once per frame
     void Update () {
         time = time - Time.deltaTime;
         time2 = time2 - Time.deltaTime;
         if (time2 <= 0.0f) {
             time2 = 1f;
             conta = conta + 0.15f;    
         }
         if (time <= 0) {
 
             time=5f - conta;
             if (time<=1f) time= 1f;
             rnd= Random.Range(1,4);
             switch (rnd) {
                         case 1:
                                 ListaOggetti[contaOggetti] = Instantiate (ret, new Vector3 (GameObject.Find ("Player").transform.position.x,
                                          GameObject.Find ("Player").transform.position.y + 20.0f), Quaternion.identity) as GameObject;
                                 
                                 
                                 break;
                         
                                 
                         case 2:
                                 ListaOggetti[contaOggetti]= Instantiate (tri90, new Vector3 (GameObject.Find ("Player").transform.position.x,
                                            GameObject.Find ("Player").transform.position.y + 20.0f), Quaternion.identity) as GameObject;
                                 
                                 break;
                         case 3:
                                 ListaOggetti[contaOggetti]= Instantiate (tri, new Vector3 (GameObject.Find ("Player").transform.position.x,
                                                                            GameObject.Find ("Player").transform.position.y + 20.0f), Quaternion.identity) as GameObject;
                                 
                                 break;
                                 
                         }
             maincamera.GetComponent<GuiOggetti>().OggettoDaMuovere= ListaOggetti[contaOggetti];
             contaOggetti= contaOggetti +1;
             }
     }
 }




I don't find the error, it works for the first 2-3 time, then it don't.

Here is the code of the second script (hope it can help)

 using UnityEngine;
 using System.Collections;
 
 public class GuiOggetti : MonoBehaviour {
     public Texture2D avanti;
     public Texture2D indietro;
 
 
     public GameObject OggettoDaMuovere;
     float speed = 4.0f;
     
 
     // Use this for initialization
     void Start () {
 
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKey (KeyCode.W)) {
             OggettoDaMuovere.transform.Translate (Vector3.right * speed * Time.deltaTime);
         }
         if (Input.GetKey (KeyCode.Q)) {
             OggettoDaMuovere.transform.Translate (Vector3.left * speed * Time.deltaTime);    
         }
         Rect w2Rect = new Rect (Screen.width - Screen.width / 8.3f,  Screen.height-Screen.height/1.03f, Screen.height / 6, Screen.height / 6);
         Rect a2Rect = new Rect (Screen.width / 10 - Screen.width / 12, Screen.height-Screen.height/1.03f, Screen.height / 6, Screen.height / 6);
         foreach (Touch t in Input.touches) {
                         Vector2 vec = t.position;
                         vec.y = Screen.height - vec.y;
             
             
                         if (w2Rect.Contains (vec)) {
                             OggettoDaMuovere.transform.Translate (Vector3.right * speed * Time.deltaTime);
 
                         }
             
                         if (a2Rect.Contains (vec)) {
                             OggettoDaMuovere.transform.Translate (Vector3.left * speed * Time.deltaTime);
                                 
                         }
                 }
 
 
 
 
 
 
     }
     void OnGUI() {
         
         GUI.Button (new Rect (Screen.width - Screen.width / 8.3f, Screen.height-Screen.height/1.03f, Screen.height / 6, Screen.height / 6), new GUIContent (avanti, "Button 1"));
         GUI.Button (new Rect (Screen.width / 10 - Screen.width / 12, Screen.height-Screen.height/1.03f , Screen.height / 6, Screen.height / 6), new GUIContent (indietro, "Button 2"));
 
         
     }

Thank you if you will help me

Comment

People who like this

0 Show 9
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 Em3rgency · Oct 18, 2014 at 06:57 AM 1
Share

I dont see anything wrong with your code at a glance, but if you say it works only sometimes, the only reason I can think of is that one of the 3 switch cases are broken somehow.

I suggest you Console.Log(rnd); right before the Switch(rnd); and see if it breaks on any specific case. If so, you'll know where the problem is more exactly.

Finally, in the future, you should try to code in english, as that will make it easier for people to understand code, whenever you need help :)

avatar image RobotReebot · Oct 18, 2014 at 02:37 PM 0
Share

I try your suggestion on my code. rnd is ok, the problem is in somewhere the case

The error appear in the 3 or 4 or 5 spawn, and it persist after he appear. Can be a bug of unity?

avatar image awest · Oct 18, 2014 at 03:25 PM 0
Share

I doubt this is a unity bug. I think refactoring your time system may help, but it could easily be a problem with one of the prefabs. What is actually going wrong? Does the code just stop? Is there an error message?

Side note. You should not be using GameObject.Find so many times. It's very inefficient. Even more so that you are using it twice in the same line to find the same object.

If Player doesn't change set a variable at the beginning and use GameObject.Find once at the start.

avatar image RobotReebot · Oct 23, 2014 at 01:00 PM 0
Share

Guys i have really no idea what to do. I create a variable player at star and assign it to Gameobject.find("Player") but it still don't work.

I have no error, no error message, i can't just move object. I really have no idea of what to do

P.S I can't help myself with debug cause GetComponent is not supported by the debugger

avatar image DerickTP · Oct 23, 2014 at 01:17 PM 0
Share

Also I guess that the GuiOggetti of the main camera doesn't change does it? In that case, you should get it in the start function, assign its reference to a variable and use instead of looking for it each frames. I think this would help you decipher whether it is the get component that doesn't work. If it changes often, then I find it a good debugging practice to break down the code in multiple lines. You could then use some code to check whether it is the get component function that crashed. It's always a good debugging practice, IMHO, to include these kind of checks when using the GetComponent, Find, FindWithTag functionalities of Unity. Hope this helps...

Best

Show more comments

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.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Referencing in a prefab 1 Answer

How does instantiate connect to rigidbody?,How does instantiate userigidbody without getcomponent? 1 Answer

Getting instantiated clone's coordinates from the same script 1 Answer

To getComponent() or Instantiate ? 2 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