0

I'm trying to get a Float variable from the script MainCharacterVarsScript, I would use this Float in this current script. The variable is called CharacterSpeed, both scripts are on the Game Object BaseCharacter. Right now I have a kinda backwards way of doing it trying to remake it into a new variable on this script. This is what I have currently, just me trying to get the component from the other script. I am getting the error on line 15 (the long one) telling me that I am using an unassigned local variable.

Condensed version of what I want:

To get a var from one script to another

1)Get float CharacterSpeed from MainCharacterVarsScript

2)Make it a usable float in my current script

3)Both on Game Object BaseCharacter

Thanks for reading and helping

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MainCharacterMove : MonoBehaviour {


float MoveSpeed;

// Use this for initialization
void Start () {


    GameObject BaseCharacter = GameObject.Find ("BaseCharacter");
    MainCharacterVarsScript mainCharacterVarsScript = 
mainCharacterVarsScript.GetComponent<MainCharacterVarsScript>();
    mainCharacterVarsScript.CharacterSpeed = MoveSpeed;
}

// Update is called once per frame
void Update () {

}
}
Chody
  • 1
  • 1
  • 1
    Possible duplicate of [Accessing a variable from another script C#](https://stackoverflow.com/questions/25930919/accessing-a-variable-from-another-script-c-sharp) – ryeMoss Oct 27 '18 at 02:45
  • I think I might've solved it by changing mainCharacterVarsScript before the .GetComponent to BaseCharacter but now I'm having an error with another script – Chody Oct 27 '18 at 02:47
  • In OO, you communicate data between objects by calling a method that returns the desired data, this is the main mean of communicating "messages" between "objects". Your code does not clearly show the 2 class definitions you want to communicate data between. – NoChance Oct 27 '18 at 03:14
  • `2)Make it a usable float in my current script` and `3)Both on Game Object BaseCharacter` what do you mean? – derHugo Oct 27 '18 at 04:50
  • *`3)Both on Game Object BaseCharacter`* Then why are you calling `GameObject.Find ("BaseCharacter")`? Just use `this.GetComponent<>()` – Draco18s no longer trusts SE Oct 27 '18 at 14:39

1 Answers1

-1
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class MainCharacterMove : MonoBehaviour
{    
    float MoveSpeed = 0f;

    // Use this for initialization
    void Start()
    {
        GameObject BaseCharacter = GameObject.Find("BaseCharacter");
        MainCharacterVarsScript mainCharacterVarsScript = mainCharacterVarsScript.GetComponent<MainCharacterVarsScript>();
        MoveSpeed = mainCharacterVarsScript.CharacterSpeed;
    }

    // Update is called once per frame
    void Update()
    {

    }
}
Bhavin Panara
  • 428
  • 4
  • 11
  • Could you please explain what you changed and why it should solve the problem? *Spoiler: Initializing the `MoveSpeed` value doesn't solve it since this variable will be initialized with the value `0` anyway.* The original issue was more that `mainCharacterVarsScript = mainCharacterVarsScript.GetComponent...` makes not much sense... – derHugo Oct 29 '18 at 05:34
  • He wants to use the 'CharacterSpeed' as 'MoveSpeed'. So, initializng do not help, but last line of Start method will solve the problem of OP according to whatever I have understood. – Bhavin Panara Oct 29 '18 at 09:21