- Home /
Script to recive varible from toher script and do something with it
I would like to know hot to access a variable from another script. Also, is it possible to take a variable in another script and assign it to a variable in the current script, like if I had a variable named (health = 50) and I wanted to set this variable to another variable in my script named health1. So that if (health1) changes so dose (health)
EX. Would the following script work, are there any problems with it.
var health1 = gameObject.Find(Player1).GetComponent(health script).health
function Update () {
}
function OnCollisionEnter(Collided : Collision){
if(Collided.gameObject.name == "Wall")
health-=1;
{
Answer by Julien-Lynge · Jul 17, 2012 at 07:37 PM
Your post comes across as you stating what you want done and demanding that someone here do it for you. I'm not sure if this was your intention, but that's now how UnityAnswers works. We're glad to help you, but we're not going to write your game for you. If that's what you're looking for, you can certainly hire someone on the forums.
That being said, people are absolutely right to point you to the Unity API and documentation. If you don't understand the Unity API, the first thing to do is to learn a bit about it before you try programming a complex script. If you don't understand the documentation within the reference pages, you may want to take a step back and invest some time in learning to program in JS or C#. There are many, many tutorials out there that walk you through simple programming, introduce variables and data structures, and will give you everything you need to know. When you've done that, then you should be able to follow the Unity GUI scripting guide (http://docs.unity3d.com/Documentation/Components/GUIScriptingGuide.html) and examples.
Good luck to you!
BTW, I presume this is the API link that you looked at. If not, this is definitely what you're looking for:
http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
And just in case I'm way off base and you're a good programmer but just confused by Unity-specific syntax, there are quite a few good video tutorials out there that can help you. Try 3DBuzz, for instance:
http://www.3dbuzz.com/vbforum/sv_home.php
Click on Unity, and then Unity Fundamentals.
Sorry if you thought I wanted you to write the script for me. I would just like to know how to access a variable from another script. I don't want anyone to write my script for me, that would not help me at all. What I would like is for someone to tell me how to use the (Get Component) function to get a variable.
I have read the API on dot syntax but what I don't understand is after I get the script how do I get the variable in the script I want to use. And I am also confused about how I get a game object then get a script in that object and then access a variable in that script.
Your answer
Follow this Question
Related Questions
set a var to a function in another script 2 Answers
custom variable type? 1 Answer
Changing a script value 1 Answer
How to Link Scripts Correctly? 1 Answer
On var enter 1 Answer