2

I am currently trying to pass a variable between a script in unity, and all of the questions previously posted seem to not help, if anything have just confused this issue more than it was intended to be.

My issue is, I have a integer named count in my PlayerController Script, and the variable of count is needed in another script called BounceObject to be used within an if statement to compare against.

This is my Player controller script, with some parts of the code missing.

public int count;

void SetCountText()
{
    countText.text = "Score: " + count.ToString (); 
    if (count >= 12)
    {
        winText.text = "You have won!";
    }
}

This is the script that I am trying to access the variable of count in, named BounceObject

public PlayerController script;
void Start()
 { 
    script = getComponent<PlayerController>();
 }

void Update()
 {

if (script.count >= 8)
{
    #Do Stuff here...
}

 }

I am not sure if I have made a mistake within my code or not. For reference the BounceObject Script is only found in a prefab which I then import into my game. The integer of count will also change so it is needed to be updated in the BounceObject script when it is changed in PlayerController.

Many thanks!

7
  • 2
    Is the PlayerController a component of the same game object to which BounceObject is attached? Commented Feb 26, 2016 at 16:58
  • Possible duplicate of Accessing variable from other script Commented Feb 26, 2016 at 17:39
  • IF you use GetComponent, you MUST NOT mark the variable "public". IF you mark the variable "public" you MUST NOT use GetComponent. (if you mark it public, you must DRAG IT IN THE EDITOR to the slot - you know)? Commented Feb 26, 2016 at 17:41
  • INCREDIBLY FULL ANSWER: stackoverflow.com/a/35555776/294884 Commented Feb 26, 2016 at 17:42
  • @Taelsin No it is not in the same game object Commented Feb 26, 2016 at 17:46

1 Answer 1

4

Assuming your PlayerController is on a separate object than your BounceObject script, you will need to specify which game object you want to get a component from. If you give your player object a tag that only it will have you can do this:

public PlayerController script;
void Start() { 
    script = GameObject.FindWithTag("Player1").GetComponent<PlayerController>();
}

Otherwise you'll need to use different methods of getting the player game object before getting its PlayerController script.

References I used: FindWithTag and GetComponent

Sign up to request clarification or add additional context in comments.

5 Comments

please please, do not answer million-times duplicates
@Taelsin How does this work for Prefabs? Does it work just the same?
@Robertgold It should work the same for prefabs. Think of prefabs as templates for game objects
Will test this later, will report what happens if anything goes wrong!
This will work as for Starters. But soon, you might want to use a List to convey your objects and select from them. Findwithid or Findwithtag is "Very Slow".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.