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 domigue · Feb 06, 2014 at 05:33 PM · get component

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...

Comment
Add comment · Show 1
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 fafase · Feb 06, 2014 at 05:34 PM 0
Share

Script is way too long. Narrow down to the relevant part and you will get help faster.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
 }
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.

Follow this Question

Answers Answers and Comments

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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