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

Calling iTween Method from other script C#

Discussion in 'Scripting' started by barrylachapelle, Oct 29, 2015.

  1. barrylachapelle

    barrylachapelle

    Joined:
    Oct 22, 2015
    Posts:
    4
    Hi all.

    I have been struggling to call a method from a different script. The method Im trying to call is a simple iTween call - I am not sure if iTween is what is screwing me up. My code...

    Call from this script:

    Code (csharp):
    1.  
    2.  
    3. usingUnityEngine;
    4. usingSystem.Collections;
    5.  
    6. public classTeleport : MonoBehaviour {
    7.  
    8. void Start() {
    9.  
    10.          //CALL METHOD HERE (Method is on an object named 'sphere')
    11.          SphereMove ScriptToAccess = transform.Find("Sphere").GetComponent("SphereMove") as MonoBehaviour;
    12.          ScriptToAccess.tweenSphere ();
    13.  
    14. }
    15.  
    16. void Update() {
    17. }
    18.  
    19. }
    20.  
    21.  
    Code on object Sphere:

    Code (csharp):
    1.  
    2.  
    3. usingUnityEngine;
    4. usingSystem.Collections;
    5.  
    6. public class SphereMove : MonoBehaviour {
    7.  
    8. voidStart(){
    9.      //i can call tweenSphere() successfully from here
    10. }
    11.  
    12. voidUpdate(){
    13. }
    14.  
    15.   public void tweenSphere(){
    16.  
    17.      iTween.MoveBy(gameObject,iTween.Hash(
    18.           "x" , 5,
    19.          "y" , 5,
    20.          "z" , 5,
    21.          "time", 20
    22.      ));
    23.  
    24.   }
    25.  
    26. }
    27.  
    28.  
    And my error...
    Code (csharp):
    1.  
    2. Assets/Cardboard/DemoScene/Teleport.cs(39,28): error CS0266: Cannot implicitly convert type `UnityEngine.MonoBehaviour' to `SphereMove'. An explicit conversion exists (are you missing a cast?)
    3.  
    4.  
    Any thought at what I am doing wrong? Thank you!
     
  2. 9miho

    9miho

    Joined:
    Oct 26, 2015
    Posts:
    1
    Try to run this code.
    // SphereMove ScriptToAccess= transform.Find("Sphere").GetComponent("SphereMove")asMonoBehaviour;
    SphereMoveScriptToAccess=transform.Find("Sphere").GetComponent<SphereMove>();
     
  3. barrylachapelle

    barrylachapelle

    Joined:
    Oct 22, 2015
    Posts:
    4
    Thanks @9miho I am still getting an error ... line 39 being the line that you suggested to change. I verified that the object is Spere and the script is SphereMove... any ideas? Thank you.

    Code (csharp):
    1.  
    2. NullReferenceException: Object reference not set to an instance of an object
    3. Teleport.Start () (at Assets/Cardboard/DemoScene/Teleport.cs:39)
    4.