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

Passing class into function...

Discussion in 'Scripting' started by Genkidevelopment, Feb 19, 2015.

  1. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    I have a class Blueprint...
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BluePrint : MonoBehaviour
    5. {
    6.     public bool Name;
    7.     public float Power;
    8.     public Vector3 Position;
    9.     public int Index;
    10.     public bool TeamA;
    11. }
    I am creating an instance of the class Blueprint on each gameobject and assigning that individual gameobjects data... For the purpose of creating functions that take the argument 'BluePrint'.

    Can that be done... What I am after might look like this?

    Code (CSharp):
    1. public void SomeAction (class BluePrint)
    2. {
    3. int Age = BluePrint.Age;
    4. float Power = BluePrint.Power;
    5. print(Age);
    6. print(Power);
    7. }
    Thanks all :D
     
  2. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    It would be just like any other variable.
    Code (csharp):
    1. public void SomeAction (varType varName)
    In your case
    Code (csharp):
    1. public void SomeAction (BluePrint bluePrint) {
    2.     int Age = bluePrint.Age;
    3.     // etc, etc
    4. }
     
  3. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Thanks Dan - A prompt reply as expected :D

    Ok, so the type 'class' can't actually be passed as an argument, thus we use 'var'? 'var' being an 'unkown' until i'ts told so -to-speak?

    Appreciated...
     
  4. vintar

    vintar

    Joined:
    Sep 18, 2014
    Posts:
    90
    The var IS known, it is a var of type BluePrint :)
     
    Genkidevelopment likes this.
  5. orb

    orb

    Joined:
    Nov 24, 2010
    Posts:
    3,033
    There is no type "class". The keyword "class" defines a type. You should take a look at the C# learning resources over on MSDN.
     
  6. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Thanks, that was what I was doing, but monodevelop kept putting a red line under it...

    I think its working now... Thanks very much
     
  7. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    I am getting the following fault!

    Code (CSharp):
    1. You are trying to create a MonoBehaviour using the 'new' keyword.  This is not allowed.  MonoBehaviours can only be added using AddComponent().  Alternatively, your script can inherit from ScriptableObject or no base class at all
    2. UnityEngine.MonoBehaviour:.ctor()
     
  8. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    You'll need to post the code for us to help with that - nowhere in the code you've already posted do you use the new keyword.

    What it'll probably end up being is you should be using AddComponent.
     
    Genkidevelopment likes this.
  9. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Ok! Thanks for the help...

    I was just reading about it after a google search... led straight to unity answers ;)

    Here is my code...

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class HPlayer4DataScript : MonoBehaviour
    5. {
    6.     //======= initialising new instance of Player Blueprint =========
    7.     PlayerBluePrint Home4 = new PlayerBluePrint();
    8.    
    9.     public Transform HomeRabbit4;  
    10.     private Transform CacheHomeRabbit4;
    11.  
    12.     // Referencing the players Rabbit
    13.     public Vector3 RabbitPosition;
    14.  
    15.     public RectTransform HShirt4;
    16.     private RectTransform CacheHShirt4;
    17.  
    18.     public float ShirtPositionXAxis;
    19.     public float ShirtPositionYAxis;
    20.    
    21.     // Referencing the OtherScript functions functions
    22.     public HomeRabbitPositionScript FromHomeRabbitPositionScript;
    23.     public GetZoneFromShirtPositionScript FromGetZoneFromShirtPositionScript;  
    24.     public PlayerAIManagerScript FromPlayerAIManagerScript;
    25.  
    26.     void Awake()
    27.     {
    28.         CacheHomeRabbit4 = HomeRabbit4;
    29.         CacheHShirt4 = HShirt4;
    30.         // Intitialising the Blueprint valuse specific to this Player...
    31.         Home4.PlayerIndex = 3;
    32.         Home4.HomeTeamPlayer = true;
    33.         Home4.RabbitPosition = RabbitPosition;
    34.        
    35.         Home4.Speed = 10.2f;
    36.        
    37.         Home4.firstName = "Denzel";
    38.         Home4.surname = "Stephens";
    39.         Home4.Nationality = "English";
    40.         Home4.Age = 25;
    41.        
    42.         //Home2.SayHello();
    43.     }
    44.  
    45.     void FixedUpdate ()
    46.     {
    47.         Home4.RabbitPosition = RabbitPosition;
    48.  
    49.         // Calclulating shirt position X and Y axis
    50.         ShirtPositionXAxis = CacheHShirt4.transform.position.x;
    51.         ShirtPositionYAxis = CacheHShirt4.transform.position.y;
    52.        
    53.         if (ShirtPositionYAxis < 521) // If selected we need to fire up his Rabbit, tag his zone and call AI manager
    54.         {
    55.             // Calling To calculate Rabbit Position
    56.             FromHomeRabbitPositionScript.GetRabbitPosition (ShirtPositionXAxis, ShirtPositionYAxis);
    57.             // Convert Rabbit Position
    58.             RabbitPosition = CacheHomeRabbit4.transform.position;
    59.             // Setting Zone Tag
    60.             FromGetZoneFromShirtPositionScript.GetZoneFromShirt (ShirtPositionYAxis);
    61.             // Calling AI Manager
    62.             FromPlayerAIManagerScript.PlayerAIManager (Home4); // Call function of AIManager
    63.         }
    64.         // else // Player is sub and We don not need to calculate any further
    65.         // Position player transform on subs bench...
    66.         //could add extra details here, like player warming up along side of pitch!
    67.     }
    68. }
     
  10. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Incidentally - the game is running smoother than ever and functioning well...

    The code above, is for a specific gameobject...(Home4) It is the start in the AI node/state tree thing I have going on...
     
  11. Timelog

    Timelog

    Joined:
    Nov 22, 2014
    Posts:
    528
    Code (CSharp):
    1. PlayerBluePrint Home4 = new PlayerBluePrint();
    This is the problem. I guess PlayerBluePrint is a MonoBehaviour, which means it is expected to be a game object and thus can not be instantiated with the new keyword.

    By glancing over your code, I guess you can create a prefab gameobject, and instantiate that in this code.
     
  12. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    If PlayerBluePrint doesn't need to be able to attach to a GameObject, don't inherit MonoBehaviour.

    Code (csharp):
    1.  
    2. public class PlayerBluePrint // Do not add : MonoBehaviour
    3. {
    4.  
    5. }
    6.  
     
  13. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    Thanks for the input.

    I have not looked into prefabs in my two months learning, I will try to study them over this weekend (and will have a cheeky check now ;) )

    Regards removing the inheritance from MonoBehavior...

    This has removed all of the warnings and the game still runs well... Only now I get two new console messages!

    Code (CSharp):
    1. GameObject (named 'PlayerBlueprint') references runtime script in scene file. Fixing!
    2.  
    3. The class named 'PlayerBluePrint' is not derived from MonoBehaviour or ScriptableObject!
    4.  
    I do have the PlayerBlueprint Script on an otherwise empty gameobject in my unity scene. I was under the understanding that scripts needed to be on an object to work ?:/
     
  14. Genkidevelopment

    Genkidevelopment

    Joined:
    Jan 2, 2015
    Posts:
    186
    So removing that object from the scene cured it, now my BluePrint script is attached to nothing, but still working!? I thought that was impossible, so some tutorials are telling me!?

    Cheers for the help again :D