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 Xeong-Hu · Mar 28, 2014 at 12:24 AM · falsetrue

How can I activate a scripts variable from another gameobject?

I didn't really know how to describe it in that question, but this is what I really mean.

Alright, I'm trying to disable a variable from another script.

For example. If this GameObject == Clicked then.

Inside Script SummonButton. Summon = false;

Bottom line I'm really trying to find out a way to disable Summoning because of a card effect.

Game View Ex:

Player activated the Card "OFF Boundaries"

Opponent is Unable to Summon.

But idk how to do this..

Another visual of how this works is. My Script is a global function. I placed my Script SummonButton in an empty GameObject The SummonButton script is always active, and what it does is this.

If I click a card that is tagged Monster Card it'll show up a Gui Button named Summon. If I click Summon then it'll apply it's effect and what not.

Since I will be having cards that'll effect the way my Summoning Button can and can't do. and cards that'll effect other things like drawing and shuffling, or disabling other stuff. I'd really like to know how I can do this or If it's possible.

Can someone please give me a hand?

Here's what I tried to do in a Cube Gameobject.

 var Jeq : MonoScript;
 
 function OnMouseDown()
 
 {
 
 
 showButton2 = false;
 }


Here's my Summoning Script Button.

 private var showButton = false;
 private var showButton2 = false;
 private var pos : Vector2;
 private var hit : RaycastHit;
 var Occupied1 : GameObject;
 var Occupied2 : GameObject;
 var Occupied3 : GameObject;
 var Occupied4 : GameObject;
 var Occupied5 : GameObject;
 var Clicked = false;
 var Zone1 : float = 3.546595;
 var Zone2 : float = 1.466332;
 var Zone3 : float = -0.5914766;
 var Zone4 : float = -2.645551;
 var Zone5 : float = 3.546595;
 var jeq : MonoScript;
     var target1: Transform;
         var speed: float;
         var Summon : boolean;
         private var selectedCard : GameObject;
         var Summoning : boolean;
         var inHand = true;
         var from : Transform;
         var to : Transform;
         
 
 
 function Update(){
 
 var step = speed * Time.deltaTime;
     if (Input.GetMouseButtonDown (1) && showButton){
         showButton = false;
         showButton2 = false;
     }                                 
     
     if (Input.GetMouseButtonDown (0) && !showButton)
     {    
         CheckClick ();
 
     }
             if (selectedCard.transform.position == target1.position){
         Summon = false;
         Screen.lockCursor = false;
         CheckClick ();}
     if (Summon == true)
     
     {
     
         
          selectedCard.transform.rotation =
           Quaternion.Lerp (from.rotation, to.rotation, Time.deltaTime * speed);
         selectedCard.transform.position = Vector3.MoveTowards(selectedCard.transform.position, target1.position, step);
        {print("I have been Summoned!");}
        Screen.lockCursor = true;
     }
 
 }
 
 function OnGUI() {
     var e = Event.current;
  
     var x = pos.x - 50;  // Calc x and y to center of button
     var y = pos.y - 10;
 
     if (showButton && GUI.Button (Rect (x,y - 135, 100, 20), "Summon")) {
     print("Summoning");
         Summon = true;
         showButton = false;
         showButton2 = false;
     }
         if (showButton2 && GUI.Button (Rect (x,y - 110, 100, 20), "Set")) {
     print("Summoning");
         Setting = true;
         showButton2 = false;
         showButton = false;
     }
 }
  
 function CheckClick() {
 
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     if (Physics.Raycast(ray, hit)) {
     Debug.Log("Card selected:"+hit.collider.name);
            if (hit.collider.tag == "Monster Card" ){
                      inHand = true;
                      showButton = true;
                      showButton2 = true;
                     selectedCard = hit.collider.gameObject; //Store my selection
                     from = selectedCard.transform;
                     if (hit.collider.tag == !selectedCard.tag){
             showButton2 = false;
            showButton = false;
            Summon = false;
            return;
            }
            }
            if (hit.collider.tag == "Monster Card"  && selectedCard.transform.position == target1.position)
            {inHand =  false;
            showButton = false;
            showButton2 = false;
            return;
            }
            if (hit.collider.tag == "Strength Card" ){
            showButton = false;
            showButton2 = false;
            Summon = false;
            return;
            }
            
            if (hit.collider.tag == "Will Card" ){
            showButton = false;
            showButton2 = false;
            Summon = false;
            return;
            }
            
          pos = Camera.main.WorldToScreenPoint(hit.transform.position);
          pos.y = Screen.height - pos.y;  // convert from Screen to GUI
          showButton = true;
          showButton2 = true;
       
     }
     else {
        showButton = false;
        showButton2 = false;
        return;
     }
 }

Please help me I'd really appreciate it.

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 dscroggi · Mar 28, 2014 at 12:36 AM 1
Share

can use getcomponent

https://docs.unity3d.com/Documentation/ScriptReference/GameObject.GetComponent.html

avatar image Xeong-Hu · Mar 28, 2014 at 02:02 AM 0
Share

Alright thanks for replying and helping.

I tried this out but... For some reason it still doesn't make my showButton = false;

What happened now!? I did it right. (Atleast I think so..)

     var clicked = false;
     
     function On$$anonymous$$ouseDown()
     {clicked = true;}
     
     function Update () {
     if (clicked == true){
         // To access public variables and functions
         // in another script attached to the same game object.
         // (ScriptName is the name of the javascript file)
         var other : SummonButton = gameObject.GetComponent(SummonButton);
         // Call the function DoSomething on the script
         other.CheckClick ();
         other.Awake();
         other.OnGUI();
         // set another variable in the other script instance
         other.showButton2 = false;
         other.showButton = false;
         }
     }

the reason why I put other.CheckClick, Awake, and OnGUI is because my showButton appears in those 3 areas.

is that necessary?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by joedartjr · Mar 28, 2014 at 02:37 AM

 //Create a variable for use with your Cube Object later
 var cube : GameObject;
 
 //Calls the SummonButton script from the Cube Object on script Start
 function Start()
 {
 //Uses the variable cube to reference the GameObject Cube
 cube = GameObject.Find("Cube");
 
 //Creates a new variable to reference the SummonButton script inside the Cube Object
 var other : SummonButton = cube.GetComponent(SummonButton);
 }
 
 
 //Then when you want to call your bool on the SummonButton script you would use
 other.showbutton2 = false;
 
 
 //You must have created a reference variable for the Cube Object if the two 
 //scripts are not on the same GameObject
 
Comment
Add comment · Show 3 · 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
avatar image Xeong-Hu · Mar 28, 2014 at 04:16 AM 0
Share

Thank you for replying dude. I tried it out and sadly..... This thing still doesn't cooperate.

This is what my code looks like now.

 //Create a variable for use with your Cube Object later
 var cube : GameObject;
 var Clicked = false;
  function On$$anonymous$$ouseDown(){
  Clicked = true;
  }
 //Calls the SummonButton script from the Cube Object on script Start
 function Start()
 {
 //Uses the variable cube to reference the GameObject Cube
 cube = GameObject.Find("Cube");
  if (Clicked == true){
 //Creates a new variable to reference the SummonButton script inside the Cube Object
 var other : SummonButton = cube.GetComponent(SummonButton);
 
  
 //Then when you want to call your bool on the SummonButton script you would use
 other.showbutton2 = false;
 other.Hello = false; 
  
 //You must have created a reference variable for the Cube Object if the two 
 //scripts are not on the same GameObject
 }
 }

When I click the box. Clicked = true. But nothing happens.

I even made a new var called Hello = true. But it doen't want to cooperate.

Why the heck is it doing this?

avatar image Xeong-Hu · Mar 28, 2014 at 08:07 AM 0
Share

Alright after i searched some similar questions i finally figured it out.

avatar image joedartjr · Mar 28, 2014 at 06:25 PM 0
Share

Yeah, the script you gave has the definitions for "if(Clicked=True)" inside of the "Start" function; which inherently runs one-time when the script is loaded. This means it was checking to see if the "Clicked" bool was set to "True" on load; when you were defining the "Clicked" bool as "False" in your reference variable. You are also defining the variable for "other" inside of an "if" statement, which is bad form. That should be moved out of it and into the base of the "Start" function. You should have also moved your:

 if (Clicked == true){
 other.showbutton2 = false;
 other.Hello = false;
 }


Into an "Update" function, which checks the value every frame. I didn't realize you wanted the script actually written for you; I would have just done so to begin with. This is what the script to control that button should have looked like using my answer above:

 var cube : GameObject;
 var Clicked = false;
 
 function Start()
 {
 cube = GameObject.Find("Cube");
 var other : SummonButton = cube.GetComponent(SummonButton);
 }
 
 function On$$anonymous$$ouseDown()
 {
 Clicked = true;
 }
 
 function Update();
 {
 if(Clicked == true)
 {
 other.showbutton2 = false;
 other.Hello = false; 
 }
 }

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

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

Related Questions

Boolean while looking at a game object? 1 Answer

Why does this simple script not work????? 0 Answers

SetActive - setactive(true) for one object, setactive(false) to the rest 1 Answer

yield waitforseconds false/true 2 Answers

How can I make 2 objects that are close to each other being treated as being in the same position? 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