Search Unity

[C# Scripts] Activate an script to an other specefic gameObject, it don't works

Discussion in 'Scripting' started by menarlinaticy, Sep 27, 2016.

  1. menarlinaticy

    menarlinaticy

    Joined:
    Jun 16, 2016
    Posts:
    26
    Hey guys...

    I am learn Unity3D, i use the personal edition...

    I have an problem with scripting to activate "scr_obj_cylinder_rotate" into "obj_cylinder",
    i get only push "scr_obj_block" into "obj_block" and i want to activate "scr_obj_cylinder_rotate"
    via an GetComponent function to "obj_cylinder"... i want to learn, how i can activate scripts
    to other specefic gameObject with the following way as example:



    -------------------------------------------------------------------------------------------------------This is "scr_obj_block" --------
    using UnityEngine;
    using System.Collections;

    public class scr_obj_blok : MonoBehaviour
    {
    void Update ()
    {
    if(Input.GetKeyUp(KeyCode.J))
    {
    gameObject.Find("obj_cylinder").GetComponent<scr_obj_cylinder_rotate>.enabled = true;
    }

    if(Input.GetKeyUp(KeyCode.L))
    {
    gameObject.Find("obj_cylinder").GetComponent<scr_obj_cylinder_rotate>.enabled = false;
    }
    }
    }
    -------------------------------------------------------------------------------------------------------------------------------------------------


    I use this script for rotate "obj_cylinder" thad i want to activate this from the "scr_obj_block" script...

    ---------------------------------------------------------------------------- This is "scr_obj_cylinder_rotate" ------------------------
    using UnityEngine;
    using System.Collections;

    public class scr_obj_cylinder_rotate : MonoBehaviour
    {
    // Update is called once per frame
    void Update ()
    {
    transform.Rotate(2,4,0, Space.World);
    }
    }
    -------------------------------------------------------------------------------------------------------------------------------------------------

    I have try this to learn activate other scripts to other specefic gameObject... but i got errors
    All scripts are written in C#, for the UnityEngine.

    But sorry for write my scripts... i don't know how i can writted in code tags...

    Can you help me for an script, to activate my "scr_obj_cylinder_rotate" script to "obj_cylinder",
    only via "scr_obj_block" ?...

    Thanks, Jamie van Cadsand.
     
    Last edited: Sep 27, 2016
  2. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    You write with code tags using this

    First, it will really help you if you will learn some naming conventions

    Now, I don't really understand what doesn't work.

    However you can disable/enable scripts using both ".enabled = true" or ".gameObject.SetActive(true)"

    You can read online how both of them work: enabled, SetActive
     
  3. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    You definitely have errors with "GetComponent" because it requires parentheses after the type.

    GetComponent<type>()
     
    CloudKid likes this.
  4. menarlinaticy

    menarlinaticy

    Joined:
    Jun 16, 2016
    Posts:
    26
    OK, but i don't know what you meant... can you give me the script pleace ?...
     
  5. CloudKid

    CloudKid

    Joined:
    Dec 13, 2015
    Posts:
    207
    Maybe you should watch this, so you can actually understand what you are dooing.
     
  6. menarlinaticy

    menarlinaticy

    Joined:
    Jun 16, 2016
    Posts:
    26
    I get now this script, to destroy another specefic gameObject from the gameObject where i put the script into... i have seeing this from the scripting API, but it still don't work to destroy another specefic gameObject... however... by the scripting API, it is an little bit other, there it is no script to destroy another gameObject... but my own script its yet to destroy another gameObject...

    Here is my own script (C#):


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class scr_obj_bullet : MonoBehaviour
    5. {
    6.     // private the object thad must be destroyed
    7.     private GameObject dest;
    8.  
    9.     // Start to find the the object thad must be destroyed
    10.     void Start ()
    11.     {
    12.         // Find this object
    13.         dest = GameObject.Find ("obj_changer");
    14.     }
    15.  
    16.     // Check collision of the bullet
    17.     void OnCollisionEnter (Collision col)
    18.     {
    19.         // Give collision id
    20.         if (col.gameObject.tag == "obj_ground")
    21.         {
    22.             // Destroy the object thad are finded
    23.             Destroy (dest.gameObject);
    24.         }
    25.     }
    26. }
    Can anyone help me with this problem... to activate another gameObject... it don't works...

    Thanks, Jamie van Cadsand
     
    Last edited: Sep 29, 2016
  7. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    Hello, I'm not sure what the result you're looking for is, but you can find out what is going wrong by using "print" statements to see what code is running, and what values it is getting.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class scr_obj_bullet : MonoBehaviour {
    5.     // private the object thad must be destroyed
    6.     private GameObject dest;
    7.  
    8.     // Start to find the the object thad must be destroyed
    9.     void Start() {
    10.         // Find this object
    11.         dest = GameObject.Find("obj_changer");
    12.  
    13.         if(dest != null) {
    14.             print("'obj_changer' found!");
    15.         } else {
    16.             print("'obj_changer' could not be found.");
    17.         }
    18.     }
    19.  
    20.     // Check collision of the bullet
    21.     void OnCollisionEnter(Collision col) {
    22.         print(gameObject.name + " collided with " + col.gameObject.name);
    23.         // Give collision id
    24.         if(col.gameObject.tag == "obj_ground") {
    25.             print(col.gameObject.name + " has the tag 'obj_ground' -- destroying obj_changer");
    26.             // Destroy the object thad are finded
    27.             Destroy(dest.gameObject);
    28.         } else {
    29.             print(col.gameObject.name + " did not have the tag 'obj_ground'");
    30.         }
    31.     }
    32. }