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. Dismiss Notice

no access to script, although getComponent?

Discussion in 'Scripting' started by dave_pilot, Mar 14, 2015.

  1. dave_pilot

    dave_pilot

    Joined:
    Jan 23, 2015
    Posts:
    7
    hi there :)
    i have some problems with my script. in one class i have this code:

    Code (CSharp):
    1. //kabel getroffen
    2.                         previewCable = Instantiate(cable1, dock2, new Quaternion(0,0,0,0)) as GameObject;
    3.                         //kabel miteinander verbinden
    4.  
    5.                         hit.transform.gameObject.GetComponent<Cable>().connect(previewCable);//WORKS!!!
    6.                    
    7.                         previewCable.GetComponentInChildren<Cable>().connect(hit.collider.gameObject); //PROBLEM!!!!
    8.  
    9.  
    and in the other class this:

    Code (CSharp):
    1.     public List<GameObject> connections;
    2.  
    3. .......
    4.  
    5.     public void connect(GameObject otherCable){
    6.         connections.Add (otherCable);
    7.     }


    my problem is, that if i want to access the "cable" component of the previewCable Gameobject, i don't have any access to the Cable script on the previewCable object. i CAN disable and enable the script via getComponent but nothing more. funny enough the line above works without any problems, where i want to access the script of the hitted object.
     
  2. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    Do you get an error message? If it's a null reference exception try debuging it.

    Code (CSharp):
    1.  
    2. previewCable = Instantiate(cable1, dock2, new Quaternion(0,0,0,0)) as GameObject;
    3.                         hit.transform.gameObject.GetComponent<Cable>().connect(previewCable);//WORKS!!!
    4.  
    5. if( previewCable.GetComponentInChildren< Cable >() != null ){
    6. Debug.Log( "Cable found in Child of previewCable!" );
    7.  
    8. previewCable.GetComponentInChildren<Cable>().connect(hit.collider.gameObject); //PROBLEM!!!!
    9. }
    10. else if( previewCable.GetComponent< Cable >() != null ) {
    11. Debug.Log( "Cable found on previewCable!" );
    12.  
    13. previewCable.GetComponent< Cable >().connect( hit.collider.gameObject );
    14. }
    15. else {
    16. Debug.Log( "Cable could not be found!" );
    17. }
    18.  
    It will be easy to see if it can be found at all.
     
  3. dave_pilot

    dave_pilot

    Joined:
    Jan 23, 2015
    Posts:
    7


    no i don't get an error message. the reference to the gameobjects are also not null. i don't get why i can't access the script o_O

    when i run this debugging code it says: "Cable found in child of previewCable!". so it finds the cable script, but if i want to access it's values, nothing happens o_O
     
  4. dave_pilot

    dave_pilot

    Joined:
    Jan 23, 2015
    Posts:
    7
    some additional info. i CAN read from the script, but can't write to it. but when i read it i get always 0. like i get the values. before they are not initialized.
     
    Last edited: Mar 15, 2015
  5. Kona

    Kona

    Joined:
    Oct 13, 2010
    Posts:
    208
    :\ hm, sound as if the problem is in the code somewhere. Could you post the code so we can read through it? :)
     
  6. dave_pilot

    dave_pilot

    Joined:
    Jan 23, 2015
    Posts:
    7
    sure :) this is it.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class placeCable : MonoBehaviour {
    5.  
    6.     BuildControl buildControlScript;
    7.     public GameObject cable1;
    8.     GameObject previewCable;
    9.     bool cableSpawned;
    10.     RaycastHit hit;
    11.     public float mouseSensitivity;
    12.     PlayerController playerControllerScript;
    13.     CameraYMove cameraYMoveScript;
    14.  
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         //load references
    19.         buildControlScript = GetComponent<BuildControl> ();
    20.         playerControllerScript = GetComponent<PlayerController> ();
    21.         cameraYMoveScript = GetComponentInChildren<CameraYMove> ();
    22.         //set cable spawned to false cause no cable spawned yet
    23.         cableSpawned = false;
    24.         //sensitivity for cable alignment
    25.         mouseSensitivity = 50f;
    26.  
    27.     }
    28.  
    29.  
    30.     void alignCable(){
    31.         //disable camera movement while cable placement
    32.         playerControllerScript.enabled = false;
    33.         cameraYMoveScript.enabled = false;
    34.         previewCable.transform.Rotate(Input.GetAxis("Mouse X") * mouseSensitivity, 0, Input.GetAxis("Mouse Y") * mouseSensitivity);
    35.  
    36.     }
    37.  
    38.  
    39.     // Update is called once per frame
    40.     void Update () {
    41.    
    42.         //right mouse click...
    43.         if (Input.GetMouseButtonDown(1) && !cableSpawned) {
    44.             //check a bool if a cable should be build
    45.             if(buildControlScript.buildCable){
    46.                 //send ray from mouse position
    47.                 Ray ray= Camera.main.ScreenPointToRay (Input.mousePosition);
    48.                 //if raycast hit...check hitted object
    49.  
    50.                 if(Physics.Raycast(ray, out hit)){
    51.                     /chec if another cable was hit
    52.                     if(hit.transform.tag == "Elec"){
    53.                         //calculate docking points
    54.                         Vector3 dock1 = hit.transform.parent.position;
    55.                         Vector3 dock2 = hit.transform.parent.Find("KabelDock2").position;
    56.  
    57.                         //cable hit
    58.                         previewCable = Instantiate(cable1, dock2, new Quaternion(0,0,0,0)) as GameObject;
    59.                         //connect cables
    60.                         Cable cableScript = previewCable.GetComponentInChildren<Cable>();
    61.                         cableScript.connect(hit.transform.gameObject);
    62.                         Debug.Log(cableScript.connections.Count);
    63.                         hit.transform.gameObject.GetComponent<Cable>().connect(previewCable.GetComponentInChildren<Cable>().gameObject);
    64.                    
    65.                         //cable spawned! set boolean
    66. to true to align cable
    67.                         cableSpawned = true;
    68.  
    69.                     }else{
    70.                         //no cable was hit
    71.  
    72.                         //spawn cable
    73.                         previewCable = Instantiate(cable1, hit.point,new Quaternion(0,0,0,0)) as GameObject;
    74.                         //align cable to object
    75.                         previewCable.transform.rotation = Quaternion.FromToRotation(Vector3.back, hit.normal);
    76.                         //align cable
    77.                         previewCable.transform.Translate(hit.normal * previewCable.transform.localScale.z);
    78.                         previewCable.transform.parent = hit.transform;
    79.                         cableSpawned = true;
    80.  
    81.                     }
    82.                 }
    83.  
    84.             }
    85.         /left moused
    86.         }else if(Input.GetMouseButton(1) && cableSpawned){
    87.            
    88.             alignCable();
    89.  
    90.         //mouse released! activate camera movement again
    91.         }else if(Input.GetMouseButtonUp(1)){
    92.        
    93.             cableSpawned = false;
    94.             playerControllerScript.enabled = true;
    95.             cameraYMoveScript.enabled = true;
    96.             //set parent
    97.             //previewCable.transform.parent = hit.collider.transform;
    98.  
    99.         //delete cable
    100.         }else if(Input.GetMouseButtonDown(0)){
    101.  
    102.             Ray ray= Camera.main.ScreenPointToRay (Input.mousePosition);
    103.            
    104.             if(Physics.Raycast(ray, out hit)){
    105.                 //wenn ein kabel getroffen wurde
    106.                 if(hit.collider.tag == "Elec"){
    107.            
    108.                         GameObject.Destroy(hit.transform.parent.gameObject);
    109.                 }
    110.             }
    111.  
    112.         }
    113.     }
    114. }
    115.  
     
  7. dave_pilot

    dave_pilot

    Joined:
    Jan 23, 2015
    Posts:
    7
    and the cable class

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class Cable : MonoBehaviour {
    7.  
    8.     //trigger
    9.     public bool marker;
    10.     //current ampere
    11.     public float ampere;
    12.     //list for connected cables
    13.     public List<GameObject> connections;
    14.     //additional ampere
    15.     public float colliderAmpere;
    16.  
    17.  
    18.  
    19.     // Use this for initialization
    20.     void Start () {
    21.         ampere = 0f;
    22.         colliderAmpere = 0f;
    23.         marker = false;
    24.         connections = new List<GameObject>();
    25.  
    26.     }
    27.  
    28.  
    29.     //set ampere
    30.     public void setAmp(float value){
    31.        
    32.         ampere += value + colliderAmpere;
    33.         //conduct ampere
    34.         conduct (ampere);
    35.         marker = !marker;
    36.     }
    37.    
    38.     //conduct ampere to connected cables
    39.     void conduct(float value){
    40.         float partAmpere = ampere / connections.Count;
    41.        
    42.         Debug.Log (connections.Count);
    43.         foreach (GameObject otherCable in connections) {
    44.             //check if cables was updated already
    45.             if(otherCable.GetComponent<Cable>().marker != marker)
    46.                 otherCable.GetComponent<Cable>().setAmp(partAmpere);
    47.         }
    48.     }
    49.  
    50.  
    51.     public void connect(GameObject otherCable){
    52.         connections.Add (otherCable);
    53.     }
    54.  
    55.  
    56.    
    57.  
    58. }
     
  8. dave_pilot

    dave_pilot

    Joined:
    Jan 23, 2015
    Posts:
    7
    i don't get it :(

    could it be that the values i send to the cable object get there BEFORE the start function. and then the start function goes over it and deletes the values i send to the object?
     
  9. dave_pilot

    dave_pilot

    Joined:
    Jan 23, 2015
    Posts:
    7
    I found the bug :) like i said, in the cable class in the start method, i overwrote the list (connections = new list<gameobject>()) .