errors while geting INT from other obj script

So iam tryng to acces a script from other obj there is a bunch of threads on this and iw read them all but iam still geting this error so here i go

So there are 2 objects in hierachy enemy and damage counter

enemy has script Enemy ’
and damagecounter has damgecounter script

so iam tryng to acces damagecounter script for enemy and this is the way iam tryng

on the enemy obj i add this script

using UnityEngine;
using System.Collections;

publicclass Enemy : MonoBehaviour
{
public GameObject Dmgcounter;
public float damage;

voidStart()
{
damage = GetComponent<damgecounter>().currentdamge;
}

}

and for Dmgcounter i just drag and drop the damage counter obj

and this is the script i add to damgecounter object

using UnityEngine;
using System.Collections;

public class damgecounter : MonoBehaviour
{

    public float currentdamge;
    public float damage =10f;


    void Start ()
    {
        currentdamge = damage;
    }
   

    void Update ()
    {
   
    }
}

First of all, you should read about naming conventions in C#
Then what’s the error you’re getting

First of all I am not sure why there are spaces missing after “public” and “void”

Second:
damage = GetComponent().currentdamge;

This would mean that your GameObjects with an “Enemy” component also would require the Damagecounter Component.
If i understand you correctly these are on seperate GameObjects within the Scene. So you need to address the proper Gameobject first

NullReferenceException: Object reference not set to an instance of an object
Enemy.Start () (at Assets/Scripts/enemy/Enemy.cs:18)

so if i read this corectly i need both of the scripst enemy sciprt and damagecounter scrip on the same object ?

Well, unless you do something like:
myEnemyGameObject.GetComponent…

yeah it would be great if you could write some example i need to get an int from other objects script in C#

http://bfy.tw/9DL8

+1 actualy you made my day it was such a headache to get this thing work but that litle scene made my day :smile: thank you :slight_smile:

sorry to bother you again but umm …well i googled and i find something usefull

public float damage ;

        GameObject damagecounterobj = GameObject.Find ("Damagecounterobj");
        damgecounter Damgecounterscript = damagecounterobj.GetComponent<damgecounter>();
        damage = Damgecounterscript.currentdamge;

it works like a charm but the problem is it only updates once on start how can i make it update all the time and how can i make the currentdamge to a reference so i can use it other actions count find it in google :frowning:

Firs tyou should store the Damagecounterscript (BTW, still you should look at the naming conventions) in a class-level variable, then you call the damage = Damg… in the update method