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

[HELP] Creating a new object, passing it a script to use

Discussion in 'Scripting' started by Harry_Glickman, Aug 5, 2015.

  1. Harry_Glickman

    Harry_Glickman

    Joined:
    Jul 8, 2015
    Posts:
    3
    Hello all,

    I am coding in C# and using Unity 5.0.

    My program creates a number of objects from a subset of many possible object types (object1, object2, object3) with Instantiate().
    GameObject newObject = Instantiate(object, position, Quaternion.identity) as GameObject;​

    It then creates a script in that new object, depending on which type of object it is.
    newObject.AddComponent<Object1Behaviour>();​

    Now, I want to be able to pass a reference to this new script (Object1Behaviour) to newObject so that it can use methods from Object1Behaviour at a later time. However, when I try to pass Object1Behaviour directly through a function call, I get the error:

    CS0119: Expression denotes a `type', where a `variable', `value' or `method group' was expected

    Any help would be greatly appreciated.

    Thank you,
    Harry

     
  2. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    why not just in prefab set script for object instead of adding

    also your question is very unclear, you want to have refference to Object1Behaviour in Object1Behaviour? or Object1Behaviour to a gameobject newObject?

    In general, if you want to store script for refference for later use
    Code (CSharp):
    1. Object1Behaviour nameofvariabletostoreit;
    2.  
    3. thenameofvariable = newObject.AddComponent<Object1Behaviour>();
     
  3. Harry_Glickman

    Harry_Glickman

    Joined:
    Jul 8, 2015
    Posts:
    3
    I'll try to give you some more details so my question is more clear.

    I have set up my program so that there is a BasicObject prefab with a BasicObjectBehaviour script. In the scene, a Controller object creates many copies of this BasicObject, but determines their "subtype" and attaches a script for behaviour specific to that object subtype (in this case, Object1Behaviour for the subtype Object1). This new attached script has functions that I want to be able to call, but I can't figure out how to call these functions from the BasicObjectBehaviour script.

    I could do something like this:
    Object1Behaviour object1script = newObject.AddComponent<Object1Behaviour>();​

    But I don't know how I would pass this reference to the BasicObjectBehaviour script from the Controller object. Do you know how I could do this?
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Posting complete scripts will help. Especially the line throwing the error.

    I would do something like this
    Code (CSharp):
    1. OtherScript clone = ....AddComponent<OtherScript>();
    2. clone.somePublicVariable = this;
     
  5. Harry_Glickman

    Harry_Glickman

    Joined:
    Jul 8, 2015
    Posts:
    3
    Hi BoredMormon,

    I tried to do that, but I ran into an issue. When I am declaring somePublicVariable in OtherScript, what do I put as the type? I would have to put Object1Behaviour as the type in this case, but the type might also have to be Object2Behaviour or Object3Behaviour, since the BasicObject's OtherScript is common to all of the object subtypes.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    If their are multiple types you can have them all inherit from the same base class or implement a similar interface.

    Do some googling on OOP.
     
    Harry_Glickman likes this.