- Home /
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
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;
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?
Your answer
Follow this Question
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