Search Unity

I can not pass variables from one script to another

Discussion in 'Editor & General Support' started by LeiWong, Sep 29, 2018.

  1. LeiWong

    LeiWong

    Joined:
    Aug 16, 2017
    Posts:
    22
    Good morning to the community, and in advance, thanks for answering.

    My problem is that I can not pass the values from one script to another.
    I have seen 30 tutorials that explain the 1000 ways to pass the data and nothing happens.

    there are no errors in the console, or anything but no script communication to another.
    I've already updated Unity and Visual Basic, I do not know what to do anymore.
     
  2. JasonBricco

    JasonBricco

    Joined:
    Jul 15, 2013
    Posts:
    956
    You'll have to provide more information. I don't know what you've tried, what behavior results from the things you've tried, and what you're trying to do.
     
  3. LeiWong

    LeiWong

    Joined:
    Aug 16, 2017
    Posts:
    22
    The goal of my project does not matter, because that already leaves it aside when I see that I could not make it work, because apparently, the data interleaving is not working.

    I do not have to tell you what I'm trying to do, because you can put on YouTube "Passing data from one script to another" and I've done that, 30 tutorials, ... and nothing, none works.

    Most are like this:

    I create in "Empty object" and I create two scripts: "Script01" and "Script02" in C #, then I declare int, string or whatever, of course "public". And from another script I call them, with the 4 methods that in 10 ways more or less tell me how to do, but, but, but ... none works. Do not collect the data of the other script, no matter how you call it, if it is by object, by class or sendmessage and the list goes on.

    I want to know if there may be a problem with Unity, or if there may be something disabled.

    Here I leave one of the simplest I found, and even then it did not work. And I did it literally 3 times, in new projects.

     
  4. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    797
    To access one script from another you need a reference to it.

    Post your actual code here and someone will be able to help you :)
     
  5. LeiWong

    LeiWong

    Joined:
    Aug 16, 2017
    Posts:
    22
    Look que tutorial, i do the same thing, and do not work, it very short like 4min.
     
  6. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    You need to provide more information, like code and what you are doing. A video of something someone else has done is no help.

    There is definitely nothing wrong with unity, passing data between classes/objects is common and used all the time.

    Something you are doing is incorrect, we need to see/know exactly what you are doing to help you find out what you are doing wrong.
     
  7. LeiWong

    LeiWong

    Joined:
    Aug 16, 2017
    Posts:
    22
    Well, as I said before. I create the "Empty object", and the scripts. "Script01" and "Script02". and I add it to the empty object.

    Script01:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Script01 : MonoBehaviour {
    6.  
    7.     public string variableA;
    8. }
    Script02:


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Script02 : MonoBehaviour {
    6.  
    7.     public string variableB;
    8.  
    9.     public void update(){
    10.         Script01 var = GetComponent<Script01>();
    11.         variableB = var.variableA;
    12.     }
    13. }

    I run the game and I can not pass the data from Sript01 to Script02 from the inspector. as it does in the video


    thanks for answering
     
  8. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    You have to reference it. Calling get component without a reference will only look at the object that is calling it. You need to tell it what game object you are looking at. You either directly reference it with a public variable, or do a find for the object that contains the script you are looking for.

    In other words, you need to tell it where to look, there could be hundreds of object with that script.
     
  9. flashframe

    flashframe

    Joined:
    Feb 10, 2015
    Posts:
    797

    You have a typo.

    Update needs an uppercase "U"

    Code (CSharp):
    1. public void Update(){
    2.         Script01 var = GetComponent<Script01>();
    3.         variableB = var.variableA;
    4.     }
     
    zombiegorilla likes this.
  10. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    Yea if both are on the same object, it’s likely just the typo. If they are different objects, you’ll need to reference them. (And still fix the typo).
     
  11. LeiWong

    LeiWong

    Joined:
    Aug 16, 2017
    Posts:
    22
    Ok thanks, I understood.
    sorry for the inconvenience
     
  12. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,051
    This is why we ask for details and code, for these types it is almost always just a simple user mistake. Next start with the details and you’ll get an answer much quicker.

    Cheers.