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 jaffy25 · May 26, 2015 at 10:49 AM · guiinstantiateobjectruntimenot working

Referencing a variable in another script possibly not working?

Hey guys. So I've been writing a script named "swipe", and now another meant to keep score named "score". I used a public static variable "score" that was declared in "Swipe", and used in "score". The code is below. For some reason, it doesn't actually instantiate the prefab AT ALL. I also tried making "score" just another class in "Swipe", but that didn't even give me an option to define the "one-nine" gameobjects in the class "score". Any help appreciated, thanks.

This below is "Swipe.cs"

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Swipe : MonoBehaviour {
     
     //inside class
     Vector2 firstPressPos;
     Vector2 secondPressPos;
     Vector2 currentSwipe;
     
     List<GameObject> prefabList = new List<GameObject>();
     public static int score;
     public GameObject ArrowUp;
     public GameObject ArrowDown;
     public GameObject ArrowLeft;
     public GameObject ArrowRight;
     public GameObject ArrowUpMissed;
     public GameObject ArrowDownMissed;
     public GameObject ArrowLeftMissed;
     public GameObject ArrowRightMissed;
     public Animation Left;
     public Animation Right;
     public Animation Up;
     public Animation Down;
     public Animation Menu;
 
 
     
     void Start() {
     prefabList.Add(ArrowUp);
         prefabList.Add(ArrowDown);
         prefabList.Add(ArrowLeft);
         prefabList.Add(ArrowRight);
         Spawn ();
         
     }
     
     
     public void Update()
 
     {
     
 
         if (Input.GetMouseButtonDown (0)) {
             //save began touch 2d point
             firstPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
         }
         if (Input.GetMouseButtonUp (0)) {
             //save ended touch 2d point
             secondPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
             
             //create vector from the two points
             currentSwipe = new Vector2 (secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
             
             //normalize the 2d vector
 
             currentSwipe.Normalize ();
         }
 
 
             if (GameObject.Find ("ArrowUpUS(Clone)") != null) {
             
             
             
             
                 if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) {
                     Debug.Log ("up swipe");
                 Up.Play ();
                 Destroy (GameObject.Find ("ArrowUpUS(Clone)"));
                 score++;
                     Spawn ();
                 firstPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
                 secondPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
                 currentSwipe = new Vector2 (secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
 
                 }
             
                 if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) {
                     Debug.Log ("Lose");
                 UpEnd ();
                 End ();
                 }
                 //swipe left
                 if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f) {
                     Debug.Log ("Lose");
                 UpEnd ();
                 End ();
                 }
                 if (currentSwipe.x > 0 && currentSwipe.y > -.05f && currentSwipe.y < 0.5f) {
                 
                     Debug.Log ("Lose");
                 UpEnd ();
                 End ();
                 }
             
             }
         
             if (GameObject.Find ("ArrowDownUS(Clone)") != null) { 
             
                 //swipe upwards
             
                 if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) {
                     Debug.Log ("down swipe");
                 Down.Play ();
                     Destroy (GameObject.Find ("ArrowDownUS(Clone)"));
                 score++;
                     Spawn ();
                 firstPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
                 secondPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
                 currentSwipe = new Vector2 (secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
                 }
                 //swipe left
                 if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f) {
                     Debug.Log ("Lose");
                 DownEnd ();
                 End ();
                 }
                 if (currentSwipe.x > 0 && currentSwipe.y > -.05f && currentSwipe.y < 0.5f) {
                 
                     Debug.Log ("Lose"); 
                 DownEnd ();
                 End ();
                 }
             
                 if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) {
                     Debug.Log ("Lose");
                 DownEnd ();
                 End ();
                 }
             }
             if (GameObject.Find ("ArrowLeftUS(Clone)") != null) { 
             
                 //d0wn
                 if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) {
                     Debug.Log ("Lose");
                 LeftEnd ();
                 End ();
                 }
                 //swipe left
                 if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f) {
                     Debug.Log ("left swipe");
                 Left.Play ();
                     Destroy (GameObject.Find ("ArrowLeftUS(Clone)"));
                 score++;
                     Spawn ();
                 firstPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
                 secondPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
                 currentSwipe = new Vector2 (secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
                 }
                 //right
                 if (currentSwipe.x > 0 && currentSwipe.y > -.05f && currentSwipe.y < 0.5f) {
                 
                     Debug.Log ("Lose"); 
                 LeftEnd ();
                 End ();
                 }
             }
             //up
             if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) {
                 Debug.Log ("Lose");
             LeftEnd ();
             End ();
             }
             if (GameObject.Find ("ArrowRightUS(Clone)") != null) { 
             
                 //d0wn
                 if (currentSwipe.y < 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) {
                     Debug.Log ("Lose");
                 RightEnd ();
                 End ();
                 }
                 //swipe left
                 if (currentSwipe.x < 0 && currentSwipe.y > -0.5f && currentSwipe.y < 0.5f) {
                     Debug.Log ("Lose");
                 RightEnd ();
                 End ();
                 }
                 //right
                 if (currentSwipe.x > 0 && currentSwipe.y > -.05f && currentSwipe.y < 0.5f) {
                 
                     Debug.Log ("right swipe");
                 Right.Play ();
                     Destroy (GameObject.Find ("ArrowRightUS(Clone)"));
                 score++;
                     Spawn (); 
                 firstPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
                 secondPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
                 currentSwipe = new Vector2 (secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
             }
                 //up
                 if (currentSwipe.y > 0 && currentSwipe.x > -0.5f && currentSwipe.x < 0.5f) {
                     Debug.Log ("Lose");
                 RightEnd ();
                 End ();
                 }
             }
 
     }
     void End(){
         firstPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
         secondPressPos = new Vector2 (Input.mousePosition.x, Input.mousePosition.y);
         currentSwipe = new Vector2 (secondPressPos.x - firstPressPos.x, secondPressPos.y - firstPressPos.y);
         Destroy (GameObject.Find ("Game/Input"));
         Menu.Play ();
 
     }
     void UpEnd(){
         Destroy (GameObject.Find ("ArrowUpUS(Clone)"));
         Instantiate (ArrowUpMissed, new Vector3 (0, 0, 5), Quaternion.identity);
     }
     void DownEnd(){
         Destroy (GameObject.Find ("ArrowDownUS(Clone)"));
         Instantiate (ArrowDownMissed, new Vector3 (0, 0, 5), Quaternion.identity);
     }
     void LeftEnd(){
         Destroy (GameObject.Find ("ArrowLeftUS(Clone)"));
         Instantiate (ArrowLeftMissed, new Vector3 (0, 0, 5), Quaternion.identity);
     }
     void RightEnd(){
         Destroy (GameObject.Find ("ArrowRightUS(Clone)"));
         Instantiate (ArrowRightMissed, new Vector3 (0, 0, 5), Quaternion.identity);
     }
 
         void Spawn(){
 
         int prefabIndex = UnityEngine.Random.Range(0,4);// This chooses which arrow
         Instantiate(prefabList[prefabIndex], new Vector3(0,0,5),Quaternion.identity);
     }
 
 }



And this is "Score.cs"

using UnityEngine; using System.Collections; using System.Collections.Generic;

public class Score : MonoBehaviour {

 public static int firstDigit;
 public static int secondDigit1;
 public static int secondDigit;
 public static int thirdDigit1;
 public static int thirdDigit;
 public static int fourthDigit1;
 public static int fourthDigit;
 public GameObject zero;
 public GameObject one;
 public GameObject two;
 public GameObject three;
 public GameObject four;
 public GameObject five;
 public GameObject six;
 public GameObject seven;
 public GameObject eight;
 public GameObject nine;
 
 
 
 void Update(){
     
     
     
     if (Swipe.score > 999) {
         
         firstDigit = Swipe.score / 1000;
         secondDigit1 = Swipe.score - (firstDigit * 1000);
         secondDigit = secondDigit1 / 100; //USE THIS ONE
         thirdDigit1 = secondDigit1 - (secondDigit * 100);
         thirdDigit = thirdDigit1 / 10; //AND THIS ONE
         fourthDigit1 = thirdDigit1 - (thirdDigit * 10);
         fourthDigit = fourthDigit1 / 1; // AND THIS ONE
         
         fourthDigitSpawn();
         thirdDigitSpawn();
         secondDigitSpawn ();
         firstDigitSpawn();
     } else if (Swipe.score > 99) {
         
         secondDigit1 = Swipe.score;
         secondDigit = secondDigit1 / 100; //USE THIS ONE
         thirdDigit1 = secondDigit1 - (secondDigit * 100);
         thirdDigit = thirdDigit1 / 10; //AND THIS ONE
         fourthDigit1 = thirdDigit1 - (thirdDigit * 10);
         fourthDigit = fourthDigit1 / 1; // AND THIS ONE
         
         thirdDigitSpawn();
         secondDigitSpawn ();
         firstDigitSpawn();
     } else if (Swipe.score > 9) {
         
         
         thirdDigit1 = Swipe.score;
         thirdDigit = thirdDigit1 / 10; //AND THIS ONE
         fourthDigit1 = thirdDigit1 - (thirdDigit * 10);
         fourthDigit = fourthDigit1 / 1; // AND THIS ONE
         
         secondDigitSpawn ();
         firstDigitSpawn();
     } else {
         
         fourthDigit1 = Swipe.score;
         fourthDigit = fourthDigit1 / 1; // AND THIS ONE
         
         
         firstDigitSpawn();
         
     }
 }
 
 void firstDigitSpawn(){
     
     switch (firstDigit) {
         
     case 9: 
         Instantiate ( nine, new Vector3 (-3, 8, 5), Quaternion.identity);
         break;
         
     case 8:
         Instantiate ( eight, new Vector3 (-3, 8, 5), Quaternion.identity);
         break;
     case 7:
         Instantiate ( seven, new Vector3 (-3, 8, 5), Quaternion.identity);
         break;
     case 6:
         Instantiate ( six, new Vector3 (-3, 8, 5), Quaternion.identity);
         break;
     case 5:
         Instantiate ( five, new Vector3 (-3, 8, 5), Quaternion.identity);
         break;
     case 4:
         Instantiate ( four, new Vector3 (-3, 8, 5), Quaternion.identity);
         break;
     case 3:
         Instantiate ( three, new Vector3 (-3, 8, 5), Quaternion.identity);
         break;
     case 2:
         Instantiate ( two, new Vector3 (-3, 8, 5), Quaternion.identity);
         break;
     case 1:
         Instantiate ( one, new Vector3 (-3, 8, 5), Quaternion.identity);
         break;
     default:
         break;
     }
     
 }
 void secondDigitSpawn(){
     
     switch (secondDigit) {
         
     case 9:
         Instantiate ( nine, new Vector3 (-2, 8, 5), Quaternion.identity);
         break;
     case 8:
         Instantiate ( eight, new Vector3 (-2, 8, 5), Quaternion.identity);
         break;
     case 7:
         Instantiate ( seven, new Vector3 (-2, 8, 5), Quaternion.identity);
         break;
     case 6:
         Instantiate ( six, new Vector3 (-2, 8, 5), Quaternion.identity);
         break;
     case 5:
         Instantiate ( five, new Vector3 (-2, 8, 5), Quaternion.identity);
         break;
     case 4:
         Instantiate ( four, new Vector3 (-2, 8, 5), Quaternion.identity);
         break;
     case 3:
         Instantiate ( three, new Vector3 (-2, 8, 5), Quaternion.identity);
         break;
     case 2:
         Instantiate ( two, new Vector3 (-2, 8, 5), Quaternion.identity);
         break;
     case 1:
         Instantiate ( one, new Vector3 (-2, 8, 5), Quaternion.identity);
         break;
     default:
         break;
     }
 }
 
 void thirdDigitSpawn(){
     
     switch (thirdDigit) {
         
     case 9:
         Instantiate ( nine, new Vector3 (-1, 8, 5), Quaternion.identity);
         break;
     case 8:
         Instantiate ( eight, new Vector3 (-1, 8, 5), Quaternion.identity);
         break;
     case 7:
         Instantiate ( seven, new Vector3 (-1, 8, 5), Quaternion.identity);
         break;
     case 6:
         Instantiate ( six, new Vector3 (-1, 8, 5), Quaternion.identity);
         break;
     case 5:
         Instantiate ( five, new Vector3 (-1, 8, 5), Quaternion.identity);
         break;
     case 4:
         Instantiate ( four, new Vector3 (-1, 8, 5), Quaternion.identity);
         break;
     case 3:
         Instantiate ( three, new Vector3 (-1, 8, 5), Quaternion.identity);
         break;
     case 2:
         Instantiate ( two, new Vector3 (-1, 8, 5), Quaternion.identity);
         break;
     case 1:
         Instantiate ( one, new Vector3 (-1, 8, 5), Quaternion.identity);
         break;
     default:
         break;
     }
 }
 void fourthDigitSpawn(){
     
     switch (fourthDigit) {
     case 9:
         Instantiate ( nine, new Vector3 (0, 8, 5), Quaternion.identity);
         break;
     case 8:
         Instantiate ( eight, new Vector3 (0, 8, 5), Quaternion.identity);
         break;
     case 7:
         Instantiate ( seven, new Vector3 (0, 8, 5), Quaternion.identity);
         break;
     case 6:
         Instantiate ( six, new Vector3 (0, 8, 5), Quaternion.identity);
         break;
     case 5:
         Instantiate ( five, new Vector3 (0, 8, 5), Quaternion.identity);
         break;
     case 4:
         Instantiate ( four, new Vector3 (0, 8, 5), Quaternion.identity);
         break;
     case 3:
         Instantiate ( three, new Vector3 (0, 8, 5), Quaternion.identity);
         break;
     case 2:
         Instantiate ( two, new Vector3 (0, 8, 5), Quaternion.identity);
         break;
     case 1:
         Instantiate ( one, new Vector3 (0, 8, 5), Quaternion.identity);
         break;
     default:
         Instantiate ( zero, new Vector3 (0, 8, 5), Quaternion.identity);
         break;
     }
 }


Again, no errors, no warnings. It just isn't instantiating any of the numbers. Thanks

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 Fornoreason1000 · May 26, 2015 at 11:43 AM 0
Share

I can see your still a bit new here. Just a few tips

Please use appropriate tags, "WTF" and "Help me please" doesn't help anyone find this question for future referring. write tags that relate to your problem. "Instantiate and not working are better ones"

also you should probably learn what arrays are... .it will save your repeating like the same line 40 times.

loops are also good for not repeating the same code literally 40 times. there are a few types of loops.

Try To $$anonymous$$eep Your Function Names Like This ,Always Starting With A Capital Each Word.

only define variables you actually need to use e.g

  fourthDigit1 = Swipe.score; //Useless variables
   fourthDigit = fourthDigit1 / 1;
 
 //$$anonymous$$uch better, though i have no idea why you are dividing it by 1?
    fourthDigit = Swipe.score/ 1;
 
    
 
 

problems like this are due to flow errors, to make sure instantiate is even being called try adding Debug.Log statements after your call Instantiate. if you see the log statement in the Console(where the big red scary errors appear") you know Instantiate was being called.

When writing up a question on this forum be precise and specific and always do your homework. $$anonymous$$ake sure explain Adequately what your are doing, what you are having trouble with, what you trying to achieve and what attempts have you made at achieving it. This way , we can quickly get up to speed of what your problem is ins$$anonymous$$d of asking more questions in this comment section.

And finally the Question, try adding debug statements where one of your Instantiates calls are. like this

 case 1:
           Debug.Log("Instantiate one object!!! First Digit!!!");
          Instantiate ( one, new Vector3 (-3, 8, 5), Quaternion.identity);
          break;
avatar image Gnometech · May 26, 2015 at 12:14 PM 0
Share

First comment: Instantiating new objects every frame is not a good idea. Try to create a pool of objects that you reuse and just shift around, otherwise you will be in performance hell.

Second comment: Using Debug.Log is a good idea to find out if the script is running. It should do something - did you assign the "Score" script to an active gameobject in your scene?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Referencing instantiated objects at runtime 2 Answers

Have a Timer Counts To 5 sec. Then Make a Sphere Appear While Using Instantiate 1 Answer

Crashing While Instantiating at Runtime 1 Answer

Create new scene at runtime 1 Answer

changing the 3D character or character's body after picking up an object 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