- Home /
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.
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?
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
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?
Alright after i searched some similar questions i finally figured it out.
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
Follow this Question
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