how to add a script to a gameobject with some value?

i want to add a script to a gameobject with a variable that is = to damageDoneOverall.
How can i do this? and im going to do this in networking which means i am going to save this value. Should i make it a static var or what?

is damageDoneOverall declared in another script?

yes it is

there are a few ways you can do it. You could create a link to it if its on a different gameobject and get the value from the script. You could keep it static and access it without creating a link. You should look at the unity documentation.

to add a script with values how would i do that
addComponent.script(1,0,0) ?

because i see what your saying but i believe adding a script to the object would be the best idea

Just make the script and then drag and drop it on to the game object from the project in Unity.

well i dont want the script to be there in the begining and i want to remove it later.
and i added the script using the code

gameObject.AddComponent(damageOverallScript);

but i do not know how to send the variables

if I read your request correctly I believe what you need is:

gameObject.AddComponent(damageOverallScript);
…more code here…

var damage = gameObject.GetComponent(damageOverallScript);
damage.TotalDmg = 10; // the .TotalDmg is the variable name in the damageOverallScript

Referanced:

var other : ScriptName = gameObject.GetComponent(ScriptName);
// Call the function DoSomething on the script
other.DoSomething ();
// set another variable in the other script instance
other.someVariable = 5;

I’m not 100% as Im not home to test to confirm but I think thats the right path for you…

Use the generic version
var other : ScriptName = gameObject.GetComponent.<ScriptName>();Ah yes. You should store the result of AddComponent. This reference will have everything you define in the class. This is made much harder to grasp as a new user by UnityScript as you just dump code directly in to a file and there is some magic that goes on for you. In C# you actually type the class (you can do this in UnityScript too (?) I believe but it isn’t enforced). Typing the class, which is only 1 line of code and a couple of brackets makes the whole thing a lot more obvious. A component is just an instanced object of the class (which can also be refered to as a type). So, when you enter…
var number : float = 10;you are creating a class with a float in it “number”. In C# it is easier to see…

public class MyComponent : MonoBehaviour
{
    public float number = 10;
}

The “extra” text can scare some new users but there is nothing hidden here. You now know you have everything in MonoBehaviour (and you can research that now that you know it). You also know the name of your component (which must match the file name), and when you use this code it all makes more sense:

private void AddAndPrintMyComponentNumber()
{
    // In C# 'var' is different. it is a powerful way of saying 'decide 
    //    the  type for me' It is still strongly typed, and should only 
    //    be used  where the type is very obvious In this case, we 
    //    know we are  getting a type MyComponent.
    var myComp = this.gameObject.AddComponent<MyComponent>();

    // Now that we have a reference to the component on this 
    //    GameObject (not the class/type, but the actual instance on 
    //    this GameObject), use it
    Debug.Log(myComp.number);  // Get and print

    myComp.number = 20;  // Set number to a new value

    Debug.Log(myComp.number);  // Get and print the new number
}

AddComponent returns the Component that was added so the GetComponent call here is redundant. Assuming you want to set the value of a public variable called totalDamage in a script after you add the script to a GameObject.

public GameObject otherObject;

public void doStuff()
{
     MyScript myScript = otherObject.AddComponent<MyScript>();
     myScript.totalDamage = 100;
}

i tried it, its not working…

var other : ScriptName = enemy.GetComponent.();

says does not denotate a valid type if its add or get

Is your actual component/script/class called “ScriptName”?

I made the script attach but i cant change the value of a var.
i already have the enemy declared in the script.

enemy.GetComponent("InfoGiver").pain = 10;

is this wrong?
also tried a dif approach

var infoGiver = enemy.AddComponent ("InfoGiver");
infoGiver.pain = 3;

Yes. See the example code above. I wouldn’t do all that in one line though. I’m not sure you need the roundbrackets here, but…

UnityScript:
(enemy.GetComponent.()).pain = 10;

C#:
(enemy.GetComponent()).pain = 10;

^- KelsoMRK’s code is much cleaner, go with his…

where did you declare MyScript? because im getting an error with that script saying that the namespace or type is not declared
(using c#)

You have to change that to the name of your class/script/component. That was a placeholder. You said you had a variable in a script and you wanted to add it, so whatever it is called. Type it in there.

okay so it would be

NameOfAddedScript WhatIsThis? = objectReferance.AddComponent<NameOfAddedScript>(/*whats in here? would this be values or something */);

also lets just say i already had the script on it and wanted to access it with ANOTHER object/script, do i just have to change Add to Get?

I hate to give this answer, but these questions are a little too basic and not of a programming nature. You should check the docs:

I use google search to find just about everything in Unity. Just search, in Google, “Unity ”. This way you also get Unity Answers, the forums here, the code docs AND the regular docs. It has been a great resource. I hate saying this because I hate to discourage anyone from seeking knowledge. I hope this helps though.

The answer to your last question is basically “yes” though ;). And the return is the instance of the Type. I suggest reading up on Types, Classes, Instances, etc. There is a huge C# community out there with hundreds of discussions and tutorials. Gaining an understanding of these terms will aid communication and help you ask more pertinent questions as well.