Home > Mobile >  Access a variable from a script in another script
Access a variable from a script in another script

Time:01-16

First of all I want to make clear the fact that I'm not using Unity or anything related to this .

I want to get values from a variable from a script to another , but inside the same .cs file.

My variable is named spellpos

Im setting it's values in this script

     public class MyMissile: ISpellScript
etc etc

      public void CastKeg(ISpellMissile missile)
        { etc
            var spellpos = new Vector2(daspell.CastInfo.TargetPositionEnd.X, daspell.CastInfo.TargetPositionEnd.Z);
   etc 

        } etc etc

Then I want to access that spellpos value in another script

    public class GragasQToggle : ISpellScript
etc etc 

        public void OnSpellCast(ISpell spell)
        {
            var position = spellpos;

        }

How can I achieve this ?

CodePudding user response:

Try making your variable public:

public class MyMissile: ISpellScript
{
    public static Vector2 spellpos;

    public void CastKeg(ISpellMissile missile)
    { 
        spellpos = new Vector2(daspell.CastInfo.TargetPositionEnd.X, daspell.CastInfo.TargetPositionEnd.Z);
    }
}

And now you can access the variable from the other class:

public class GragasQToggle : ISpellScript
{
    public void OnSpellCast(ISpell spell)
    {
        var position = MyMissile.spellpos;
    }
}

CodePudding user response:

OR You can make the first method return the value and use in the second method like below:

public Vector2 CastKeg(ISpellMissile missile)
{ 
        var spellpos = new Vector2(daspell.CastInfo.TargetPositionEnd.X, daspell.CastInfo.TargetPositionEnd.Z);

        return spellpos;
} 

And then you call in the second method like below:

    public void OnSpellCast(ISpell spell)
    {
        var position = CastKeg(spell);

    }

CodePudding user response:

Another option is to make get and set functions for each class and leave them as private.

If those are separated programs or even the same program a creative solution is to use TCP sockets.

  •  Tags:  
  • Related