Hey I need to pass the value of a variable in one script to another script. Can I make an object of one script and use that to pass the value of the variable, or do I have to do it some other way?
See [the docs][1] about accessing other objects.
There are a quite a few ways to do it.
The easiest, and least elegant way (certainly someone may bash me for this), is to make the variable static (C#) and access them by just ScriptName.VariableName. You can access functions or methods like that as well. Good and easy for central code blocks where you have one and only one script for the game
If you do a raycast or something and hit an object and you want to get the object's script you can always get it using GetComponent and cast it to your class:
MyScript t =(MyScript)touchStartedOnObject.GetComponent("MyScript");
Then you can get direct access to the public properties and methods.
I was thinking of having a GameMainObject that will hold alot of the detail of the game so when objects are instantiated, etc, they have same parent for various reasons
baseexample.
- GameMainObject - js
- HeroGun
- Projectile-js
- EnemyObject-js
- WreckedObject
- HeroGun
I miss the ease of use that Game Maker 4 had, it was a 2D (and basic 3D) game maker/compiler. It had a few nice features, such as declaring a variable as global (for example, global.myvar = 0; ) allowed it to be viewed/edited/accessed from ANY game object. Also, it had a simple way to access objects, just putting the object and the variable you want to access (eg, PlayerObject1.isAlive = 0). I am a C++ programmer who’s a little rusty, I am trying to learn the bits I need like passing var’s. I think I will try an old method from GameMaker4, making a persistant Controller object that stays in every game scene and has full control of variables… until I learn a better way.