Skip to main content

Referencing another script..

So, here's how it's done.

function addingExp(){
    var level = gameObject.Find("Player").GetComponent(Level);
    level.experience +=50;
}

Explanation:


gameObject.Find("Player")
 Finds the object the script it attached to. Player is the name of the object, note that the name has to be in quotations.

GetComponent(Level)
Finds the name of the script.

level.experience +=50;
level is calling the variable that we just set above, experience is a variable within the script that were calling on in the code.




If you wanted to call a function from another script, it would be something like this...


function addingExp(){
    var level = gameObject.Find("Player").GetComponent(Level);
    level.DoSomething();
}

Calling on different scripts is great for any game, especially for making a point system or creating something like an RPG to keep up with levels and everything. 

Comments

Post a Comment

Popular posts from this blog

How To Make A Gun Shot Sound (SFX On Unity 3D)

When it comes to audio in Unity, there are four components: Audio Clip , Audio Source , Audio Listener , and Audio Re-verb Zone . Audio Clips are the actual audio file imported into your game. Unity supports file formats: .aif, .wav, .mp3, and .ogg. When imported, you can compress them greatly, with the price of loosing some quality. You can do this by first selecting the audio clip, view it in the inspector. Under the Audio Importer component, you can switch the audio format from Native to the audio clip, to a compressed format applied by Unity. You can change how compressed the file is by dragging the bar at the bottom, then hitting apply. You can get plenty of free good SFX from a site called  freesound.org . All you have to do is create an account for free , and download all the sounds you want. I found a nice gun shot sound here . Simply download and load into your Project. Audio Source actually plays the audio clip in your scen...

Handling Music and Sound Effects In Your Games

Initiative  While developing Treva's Adventure I had to figure out a way to handle multiple music tracks and sound effects in a clean manner or suffer horribly.  What was going to help me achieve a simple solution was taking all the different sounds and centralizing them in a single class in order to black box them.   Any other code trying to play a sound wouldn't even know the sound file's name.   All code trying to play a music track would reference a enum that defines all the track names. Defining The Class Creating The Enum When I first started defining types in my enumeration,  I was naming the types to be exactly like the file name.  For a scary sound effect I had found a file named "ghost breath".  So around my code would be scattered lines like SoundManager.Play(SoundEffectType.GhostBreath);  This was fine until I found a sound that better fit the situation it was being used in,  and decided to use "ghost breath" for a...

Top Down Shooter (Object Look At Mouse)

Sooo let's say your making a top down shooter, you want your player to look in the direction of your mouse like so: (Yeah I know, not the best picture)