Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

errors while geting INT from other obj script

Discussion in 'Scripting' started by kilix19980, Dec 31, 2016.

  1. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    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

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. publicclass Enemy : MonoBehaviour
    5. {
    6. public GameObject Dmgcounter;
    7. public float damage;
    8.  
    9. voidStart()
    10. {
    11. damage = GetComponent<damgecounter>().currentdamge;
    12. }
    13.  
    14. }
    and for Dmgcounter i just drag and drop the damage counter obj

    and this is the script i add to damgecounter object

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class damgecounter : MonoBehaviour
    5. {
    6.  
    7.     public float currentdamge;
    8.     public float damage =10f;
    9.  
    10.  
    11.     void Start ()
    12.     {
    13.         currentdamge = damage;
    14.     }
    15.    
    16.  
    17.     void Update ()
    18.     {
    19.    
    20.     }
    21. }
    22.  
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    First of all, you should read about naming conventions in C#
    Then what's the error you're getting
     
  3. vothka

    vothka

    Joined:
    Mar 27, 2015
    Posts:
    59
    First of all I am not sure why there are spaces missing after "public" and "void"

    Second:
    damage = GetComponent<damgecounter>().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
     
  4. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    NullReferenceException: Object reference not set to an instance of an object
    Enemy.Start () (at Assets/Scripts/enemy/Enemy.cs:18)
     
  5. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    so if i read this corectly i need both of the scripst enemy sciprt and damagecounter scrip on the same object ?
     
  6. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    Well, unless you do something like:
    myEnemyGameObject.GetComponent...
     
  7. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    yeah it would be great if you could write some example i need to get an int from other objects script in C#
     
  8. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    http://bfy.tw/9DL8
     
  9. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    +1 actualy you made my day it was such a headache to get this thing work but that litle scene made my day :D thank you :)
     
  10. kilix19980

    kilix19980

    Joined:
    Jun 13, 2015
    Posts:
    74
    sorry to bother you again but umm ....well i googled and i find something usefull
    Code (CSharp):
    1. public float damage ;
    2.  
    3.         GameObject damagecounterobj = GameObject.Find ("Damagecounterobj");
    4.         damgecounter Damgecounterscript = damagecounterobj.GetComponent<damgecounter>();
    5.         damage = Damgecounterscript.currentdamge;
    6.  
    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 :(
     
  11. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    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