Search Unity

How can I make an object own an instantiated object and access its variables?

Discussion in 'Scripting' started by From-Soy-Sauce, Sep 16, 2014.

  1. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Hello, I have a case where I have a knife that I want to always be at the position of the player that created it. This is the knife's code:
    Code (CSharp):
    1. using UnityEngine;using System.Collections;public class sc_spin_knife : MonoBehaviour {
    2. float rotation=0;
    3. float face=-1;
    4. GameObject master;
    5. float angle;
    6.  
    7.     void FixedUpdate ()
    8.     {
    9.     transform.position=master.transform.position;
    10.     rotation += glob.gamespeed * 15;
    11.     transform.eulerAngles = new Vector3(rotation,90*face,0);
    12.     }
    13. }
    14.  
    I want it so that the creator sets "master" from when it instantiates the knife, and I thought it would be like this, but it isn't:
    Code (CSharp):
    1. GameObject i= Instantiate(glob.OBKNIFE1,transform.position,Quaternion.identity) as GameObject;
    2.             i.master=gameObject;
     
  2. Disolution

    Disolution

    Joined:
    Feb 24, 2014
    Posts:
    71
    You can always make a new public GameObject and assign the knife prefab to it then u can always instantiate it throught the public variable u created
     
  3. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Is this an FPS or something?
    How about having each of the game's weapons under the player's hierarchy, and activate it once you get it or use it?
     
  4. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    You need to GetComponent for the script, e.g.
    line 2 would be:
    i.GetComponent("ScriptNameHere").master = gameObject;

    Hope this helps! :)
     
  5. From-Soy-Sauce

    From-Soy-Sauce

    Joined:
    Jan 7, 2014
    Posts:
    162
    Hmm getcomponent sounds right but I cannot get it to work, I tried this:
    i.GetComponent("Assets/Resources/Scripts/bullets/sc_spin_knife".master = gameObject;

    But I got this error:

    Assets/Resources/Scripts/char/CharacterSakuya.cs(274,82): error CS1061: Type `UnityEngine.Component' does not contain a definition for `master' and no extension method `master' of type `UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)


    And no, this is not for an FPS, it is for an SSB style fighter:
     
  6. Krysalgir

    Krysalgir

    Joined:
    Aug 30, 2010
    Posts:
    95
    When you instanciate your GameObject, you only get... a GameObject.
    You have to extract your MonoBehaviour script from it to access its own fields.
    1. GameObject i= Instantiate(glob.OBKNIFE1,transform.position,Quaternion.identity) as GameObject;
    2. sc_spin_knife myKnife = null;
    3. if(i != null)
    4. myKnife = i.GetComponent<sc_spin_knife>();
    5. if(myKnife != null)
    6. myKnife.master=gameObject;
     
  7. BmxGrilled

    BmxGrilled

    Joined:
    Jan 27, 2014
    Posts:
    239
    you just need:
    i.GetComponent("sc_spin_knife").master = gameObject;
    you just need the component name.
     
  8. Krysalgir

    Krysalgir

    Joined:
    Aug 30, 2010
    Posts:
    95
    Or the component type (at least in C#) ;)
    Also a few nullref checks are always nice to have, just in case you don't drag&drop the correct prefab!
     
  9. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    Well, unless it's a typo, as others here seem to think, you missed an ending bracket.
    i.GetComponent("Assets/Resources/Scripts/bullets/sc_spin_knife").master = gameObject;

    Well even if it's no an FPS, I think creating all of the possible objects in the arm, and then turning them off, only to turn them on when you equip them, is a better solution. Especially in a game like SSB where you equip and throw stuff countless times.
    This means that each character has a set number of objects created, and new ones will not be needed to be instantiated during gameplay, only turned on and off.

    Also, correct me if I'm wrong, but isn't it supposed to be i.GetComponen<"Assets/Resources/Scripts/bullets/sc_spin_knife">().master = gameObject; instead, in C#?
     
  10. Krysalgir

    Krysalgir

    Joined:
    Aug 30, 2010
    Posts:
    95
    In C# you only use the class type between the <>, and not a string for a name or location (which BTW avoids typos as the class has to exist in the project).
    In Mono or VS you can also have auto-completion thanks to that.
     
  11. MrPriest

    MrPriest

    Joined:
    Mar 17, 2014
    Posts:
    202
    I see I see, I thought so. Wanted to be sure, thanks.