- Home /
problem whit get component script JS
I try make card game in my game. Its work fine moustly, but when you win or lose, money what you get or lose not be correct... Dont know why, i try many different options but still it give wrong + or - to money. I hope somebody understand what i try explain.
GameObject.GetComponent(Script).variable += bet;
So if bet is 3, variable = variable + 3? is that correct?
static var raha = 999;
So if bet is 3, raha = raha + bet?
And there is bigger part of code: if(myFirstCard + mySecondCard == 21) { pallo.GetComponent(Player_info).raha+=panokoko; tapahtu.SetActive (true); tapahtu.GetComponent(TextMesh).text = "VOITIT"; }
now if i bet is 1 and i win it give about 15 "money" more...
If parts up there is correct, problem is somewhere else :) I link whole script, is little bit messy so dont freak out :D
#pragma strict
var pa1 : Texture;
var pa2 : Texture;
var pa3 : Texture;
var pa4 : Texture;
var pa5 : Texture;
var pa6 : Texture;
var pa7 : Texture;
var pa8 : Texture;
var pa9 : Texture;
...
...
... // there is every card text...
...
var kortti : GameObject;
var kortti2 : GameObject;
var kortti1E : GameObject;
var kortti2E : GameObject;
var number : int;
var oldnumber : int;
var a : int = 2;
var A1 : GameObject;
var A2 : GameObject;
var A3 : GameObject;
var A4 : GameObject;
var A5 : GameObject;
var A6 : GameObject;
var panokoko : int;
var panos : boolean = false;
var money : int = 0;
var rahamaara : GameObject;
var rahacovered : String;
var tapahtu : GameObject;
var ekakortti : int;
var tokakortti : int;
var ekakorttiE : int;
var tokakorttiE : int;
var gameON : boolean = false;
var valinta : boolean = true;
var playerkortti : int;
var enemykortti : int;
var CardGamera : GameObject;
var gatrigger : GameObject;
var pallo : GameObject;
function Start () {
}
function Update () {
money = Player_info.raha;
rahacovered = money.ToString();
rahamaara.GetComponent(TextMesh).text = rahacovered;
if(Input.GetKey(KeyCode.Return) && a==1)
{
pallo.SetActive (true);
CardGamera.SetActive (false);
gatrigger.SetActive (true);
}
if(Input.GetKey(KeyCode.UpArrow) && a==2)
{
a=1;
}
if(Input.GetKey(KeyCode.LeftArrow) && a==2)
{
a=3;
}
if(Input.GetKey(KeyCode.UpArrow) && a==3 && panos == false)
{
a=4;
}
if(Input.GetKey(KeyCode.RightArrow) && a==3)
{
a=2;
}
if(Input.GetKey(KeyCode.DownArrow) && a==1)
{
a=2;
}
if(Input.GetKeyUp(KeyCode.RightArrow) && a==4)
{
a=5;
}
if(Input.GetKeyDown(KeyCode.RightArrow) && a==5)
{
a=6;
}
if(Input.GetKeyUp(KeyCode.LeftArrow) && a==6)
{
a=5;
}
if(Input.GetKeyDown(KeyCode.LeftArrow) && a==5)
{
a=4;
}
if(Input.GetKey(KeyCode.DownArrow) && a==4 || Input.GetKey(KeyCode.DownArrow) && a==5 || Input.GetKey(KeyCode.DownArrow) && a==6)
{
if(panos==false)
{
A4.renderer.material.color = Color(1,1,1);
A5.renderer.material.color = Color(1,1,1);
A6.renderer.material.color = Color(1,1,1);
}
a=3;
}
if(Input.GetKeyUp(KeyCode.Return) && a==3 && panos==true)
{
if(gameON==true)
{
valinta=false;
}
WhatCard();
}
if(a==1)
{
A1.renderer.material.color = Color(1,0,0);
A2.renderer.material.color = Color(1,1,1);
A3.renderer.material.color = Color(1,1,1);
}
if(a==2)
{
A1.renderer.material.color = Color(1,1,1);
A2.renderer.material.color = Color(1,0,0);
A3.renderer.material.color = Color(1,1,1);
}
if(a==3)
{
A1.renderer.material.color = Color(1,1,1);
A2.renderer.material.color = Color(1,1,1);
A3.renderer.material.color = Color(1,0,0);
}
if(a==4 && panos == false)
{
A3.renderer.material.color = Color(1,1,1);
A4.renderer.material.color = Color(1,0,0);
A5.renderer.material.color = Color(1,1,1);
A6.renderer.material.color = Color(1,1,1);
if(Input.GetKey(KeyCode.Return))
{
panokoko=1;
panos=true;
}
}
if(a==5 && panos == false)
{
A4.renderer.material.color = Color(1,1,1);
A5.renderer.material.color = Color(1,0,0);
A6.renderer.material.color = Color(1,1,1);
if(Input.GetKey(KeyCode.Return))
{
panokoko=2;
panos=true;
}
}
if(a==6 && panos == false)
{
A4.renderer.material.color = Color(1,1,1);
A5.renderer.material.color = Color(1,1,1);
A6.renderer.material.color = Color(1,0,0);
if(Input.GetKey(KeyCode.Return))
{
panokoko=3;
panos=true;
}
}
if(a==2 && gameON==true && Input.GetKey(KeyCode.Return))
ratkaisu();
if(panokoko==1)
A4.renderer.material.color = Color(1,0,0);
if(panokoko==2)
A5.renderer.material.color = Color(1,0,0);
if(panokoko==3)
A6.renderer.material.color = Color(1,0,0);
}
function WhatCard()
{
if(valinta == true)
{
gameON=true;
var number = Random.Range(1,52);
switch(number)
{
case 1:
kortti.renderer.material.mainTexture =pa1;
ekakortti=1;
break;
case 2:
kortti.renderer.material.mainTexture =pa2;
ekakortti=2;
break;
case 3:
kortti.renderer.material.mainTexture =pa3;
ekakortti=3;
break;
...
... // and there is every case for cards
...
...
}
kortti.animation.Play();
oldnumber = number;
}
if(valinta == false)
{
number = Random.Range(1,52);
while(oldnumber == number)
{
number = Random.Range(1,52);
}
kortti2.animation.Play();
switch(number)
{
case 1:
kortti2.renderer.material.mainTexture =pa1;
tokakortti=1;
break;
case 2:
kortti2.renderer.material.mainTexture =pa2;
tokakortti=2;
break;
case 3:
kortti2.renderer.material.mainTexture =pa3;
tokakortti=3;
break;
case 4:
kortti2.renderer.material.mainTexture =pa4;
tokakortti=4;
break;
...
... // and there is every case for cards
...
...
}
}
if(valinta == true)
{
number = Random.Range(1,52);
kortti1E.animation.Play("korttie1");
while(oldnumber == number)
{
number = Random.Range(1,52);
}
switch(number)
{
case 1:
kortti1E.renderer.material.mainTexture =pa1;
ekakorttiE=1;
break;
case 2:
kortti1E.renderer.material.mainTexture =pa2;
ekakorttiE=2;
break;
case 3:
kortti1E.renderer.material.mainTexture =pa3;
ekakorttiE=3;
break;
case 4:
kortti1E.renderer.material.mainTexture =pa4;
ekakorttiE=4;
break;
...
... // and there is every case for cards
...
...
}
if(ekakorttiE<=9)
{
number = Random.Range(1,52);
while(oldnumber == number)
{
number = Random.Range(1,52);
}
kortti2E.animation.Play("korttie2");
switch(number)
{
case 1:
kortti2E.renderer.material.mainTexture =pa1;
tokakorttiE=1;
break;
case 2:
kortti2E.renderer.material.mainTexture =pa2;
tokakorttiE=2;
break;
case 3:
kortti2E.renderer.material.mainTexture =pa3;
tokakorttiE=3;
break;
case 4:
kortti2E.renderer.material.mainTexture =pa4;
tokakorttiE=4;
break;
case 5:
kortti2E.renderer.material.mainTexture =pa5;
tokakorttiE=5;
break;
...
... // and there is every case for cards
...
...
}
}
}
if(ekakorttiE + tokakorttiE > 21)
{
ratkaisu();
}
if(ekakortti+tokakortti > 21)
{
ratkaisu();
}
}
function ratkaisu()
{
if(ekakorttiE<=9)
{
kortti2E.animation.Play("korttie2k");
}
kortti1E.animation.Play("korttie1k");
enemykortti=ekakorttiE+tokakorttiE;
playerkortti=ekakortti+tokakortti;
if(playerkortti <= 22 && enemykortti < 22)
{
pallo.GetComponent(Player_info).raha+=panokoko;
tapahtu.SetActive (true);
tapahtu.GetComponent(TextMesh).text = "HÄVISIT :(";
}
if(playerkortti > enemykortti)
{
pallo.GetComponent(Player_info).raha= money + panokoko;
tapahtu.SetActive (true);
tapahtu.GetComponent(TextMesh).text = "VOITIT";
}
if(ekakortti+tokakortti > 21)
{
pallo.GetComponent(Player_info).raha= money - panokoko;
tapahtu.SetActive (true);
tapahtu.GetComponent(TextMesh).text = "HÄVISIT :(";
}
if(ekakorttiE + tokakorttiE == 21)
{
pallo.GetComponent(Player_info).raha= money + panokoko;
tapahtu.SetActive (true);
tapahtu.GetComponent(TextMesh).text = "VOITIT";
}
yield WaitForSeconds (3);
gameON=false;
panos = false;
valinta = true;
A4.renderer.material.color = Color(1,1,1);
A5.renderer.material.color = Color(1,1,1);
A6.renderer.material.color = Color(1,1,1);
kortti.transform.position.x = 0.891379;
kortti.transform.position.y = -0.001195927;
kortti.transform.position.z = 14.14625;
kortti2.transform.position.x = 0.8698014;
kortti2.transform.position.y = 0.008654209;
kortti2.transform.position.z = 14.23298;
kortti.transform.rotation.x = 86.67268;
kortti.transform.rotation.y = 13.67639;
kortti.transform.rotation.z = 195.7266;
kortti2.transform.rotation.x = 86.67268 ;
kortti2.transform.rotation.y = 13.67639;
kortti2.transform.rotation.z = 195.7266;
panokoko = 0;
tapahtu.SetActive (false);
}
It's little bit unfinished so...
Script is way too long. Narrow down to the relevant part and you will get help faster.
Answer by anmre21 · Dec 31, 2016 at 03:22 PM
I'm a beginner but I don't think that is a valid way of calling GetComponent. Instead of:
GameObject.GetComponent(Script).variable += bet;
you could use this:
var gameObject : GameObject;
var script : Script;
function Start () { //could also be done in Awake
script = gameObject.GetComponent(Script);
}
function Update () { //or whatever function you total bets in
script.variable += bet;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613132715im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to sort get components? 3 Answers
Get the color component from a raw image UI 1 Answer
null reference exception on get component 4 Answers
get collider component by raycast 0 Answers
how to create a custom type in C#? public Atype atype; 1 Answer