Search Unity

Change value of Instantiated prefabs script.

Discussion in 'Scripting' started by spil778, Jan 31, 2015.

  1. spil778

    spil778

    Joined:
    Mar 31, 2013
    Posts:
    57
    Hallo I am doing a magic creation script here. The prefab at which I instantiated have the script "EffectSettings" which holds the settings for the speed, damage, effect and so on of the spell. When I Instantiate it how can I change the variables of this new cloned prefab?

    Scripts:
    AttackTrigger Part at which I instantiate:
    Code (JavaScript):
    1. if (Input.GetButtonUp("Fire2") && !isCasting){
    2.         if(model == null){
    3.             Debug.Log("No Magic selected");
    4.         }
    5.         else{
    6.            
    7.             //newMagic = Instantiate(model, new Vector3(MagicFire,MagicFire.MagicFire.transform.position.y+1.3f,MagicFire.transform.position.z), MagicFire.transform.rotation);
    8.             transformOfMagic = MagicFire.GetComponent(Transform);
    9.             newMagic = Instantiate(model, new Vector3(transformOfMagic.position.x,transformOfMagic.position.y,transformOfMagic.position.z), transformOfMagic.rotation);
    10.             //newMagic.GetComponent.<EffectSettings>().target = radius;
    11.         }
    12.     }
    AttackTrigger:
    Code (JavaScript):
    1. #pragma strict
    2. public var model : GameObject;
    3. public var MagicFire : GameObject;
    4. private var transformOfMagic : Transform;
    5.  
    6. var mainModel : GameObject;
    7. var attackPoint : Transform;
    8. var attackPrefab : Transform;
    9.  
    10. private var atkDelay : boolean = false;
    11. var freeze : boolean = false;
    12.  
    13. var attackSpeed : float = 0.15;
    14. private var nextFire : float = 0.0;
    15. var atkDelay1 : float = 0.1;
    16. var skillDelay : float = 0.3;
    17.  
    18. var attackCombo : AnimationClip[] = new AnimationClip[3];
    19. var attackAnimationSpeed : float = 1.0;
    20. var skillAnimation : AnimationClip[] = new AnimationClip[3];
    21. var skillAnimationSpeed : float = 1.0;
    22. var manaCost : int[] = new int[3];
    23. private var hurt : AnimationClip;
    24.  
    25.  
    26. private var meleefwd : boolean = false;
    27. private var isCasting : boolean = false;
    28. private var newMagic : GameObject;
    29.  
    30. private var c : int = 0;
    31. private var conCombo : int = 0;
    32.  
    33. var Maincam : Transform;
    34. var MaincamPrefab : GameObject;
    35. var attackPointPrefab : GameObject;
    36.  
    37. var currentMagicIndex = 0;
    38.  
    39. private var str : int = 0;
    40. private var matk : int = 0;
    41.  
    42. var aimIcon : Texture2D;
    43. var aimIconSize : int = 40;
    44.  
    45. private var flinch : boolean = false;
    46. private var skillEquip : int  = 0;
    47. private var knock : Vector3 = Vector3.zero;
    48. var scriptRef : MagicSkillHolder;
    49. var magicStat : CurrentMagic;
    50. var currMagicSel : int;
    51. var radius : GameObject;
    52.  
    53. function Awake () {
    54.     scriptRef = GetComponent(MagicSkillHolder);
    55.     magicStat = GetComponent(CurrentMagic);
    56.     gameObject.tag = "Player";
    57.     if(!Maincam){
    58.         Maincam = GameObject.FindWithTag ("MainCamera").transform;
    59.     }
    60.     if(!mainModel){
    61.         mainModel = this.gameObject;
    62.     }
    63.     //Check if Main Camera does not attached ARPGcamera Script. Destroy it and spawn New Camera from MainCamPrefab
    64.     if(Maincam){
    65.         var checkCam : ARPGcamera = Maincam.GetComponent(ARPGcamera);
    66.         if (!checkCam) {
    67.             Destroy (Maincam.gameObject);
    68.             var newCam : GameObject = Instantiate(MaincamPrefab, transform.position , transform.rotation);
    69.             Maincam = newCam.transform;
    70.         }
    71.         Maincam.GetComponent(ARPGcamera).target = this.transform;
    72.     }
    73.     str = GetComponent(Status).addAtk;
    74.     matk = GetComponent(Status).addMatk;
    75.     //Set All Attack Animation'sLayer to 15
    76.     var animationSize : int = attackCombo.length;
    77.     var a : int = 0;
    78.     if(animationSize > 0){
    79.         while(a < animationSize && attackCombo[a]){
    80.             mainModel.animation[attackCombo[a].name].layer = 15;
    81.             a++;
    82.         }
    83.     }
    84.    
    85.     animationSize = skillAnimation.length;
    86.     a = 0;
    87.     //Set All Skill Animation'sLayer to 16
    88.         if(animationSize > 0){
    89.         while(a < animationSize && skillAnimation[a]){
    90.             mainModel.animation[skillAnimation[a].name].layer = 16;
    91.             mainModel.animation[skillAnimation[a].name].speed = skillAnimationSpeed;
    92.             a++;
    93.         }
    94.     }
    95.    
    96. //--------------------------------
    97.     //Spawn new Attack Point if you didn't assign it.
    98.     if(!attackPoint){
    99.         if(!attackPointPrefab){
    100.             print("Please assign Attack Point");
    101.             freeze = true;
    102.             return;
    103.         }
    104.         var newAtkPoint : GameObject = Instantiate(attackPointPrefab, transform.position , transform.rotation);
    105.         newAtkPoint.transform.parent = this.transform;
    106.         attackPoint = newAtkPoint.transform;  
    107.     }
    108.     hurt = GetComponent(PlayerAnimation).hurt;
    109. }
    110.  
    111.  
    112. function Update () {
    113.     if(freeze || atkDelay || Time.timeScale == 0.0){
    114.         return;
    115.     }
    116.     var controller : CharacterController = GetComponent(CharacterController);
    117.     if (flinch){
    118.         controller.Move(knock * 6* Time.deltaTime);
    119.         return;
    120.     }
    121.        
    122.     if (meleefwd){
    123.         var lui : Vector3 = transform.TransformDirection(Vector3.forward);
    124.         controller.Move(lui * 5 * Time.deltaTime);
    125.     }
    126.     attackPoint.transform.rotation = Maincam.GetComponent(ARPGcamera).aim;
    127.     var bulletShootout : Transform;
    128. //----------------------------
    129.     //Normal Trigger
    130.         if (Input.GetButton("Fire1") && Time.time > nextFire && !isCasting) {
    131.             if(Time.time > (nextFire + 0.5)){
    132.                 c = 0;
    133.             }
    134.         //Attack Combo
    135.             if(attackCombo.Length >= 1){
    136.                 conCombo++;
    137.                 AttackCombo();
    138.             }
    139.         }
    140.     //Magic
    141.     if (Input.GetButtonUp("Fire2") && !isCasting){
    142.         if(model == null){
    143.             Debug.Log("No Magic selected");
    144.         }
    145.         else{
    146.            
    147.             //newMagic = Instantiate(model, new Vector3(MagicFire,MagicFire.MagicFire.transform.position.y+1.3f,MagicFire.transform.position.z), MagicFire.transform.rotation);
    148.             transformOfMagic = MagicFire.GetComponent(Transform);
    149.             newMagic = Instantiate(model, new Vector3(transformOfMagic.position.x,transformOfMagic.position.y,transformOfMagic.position.z), transformOfMagic.rotation);
    150.             //newMagic.GetComponent.<EffectSettings>().target = radius;
    151.         }
    152.     }
    153.    
    154.     //Sort through Magic
    155.     if(Input.GetKeyUp(KeyCode.Z)){
    156.         if(scriptRef.magicAmount == 0){Debug.Log("No Magic");}
    157.         else{
    158.             Debug.Log(currMagicSel + "," + scriptRef.magicAmount);
    159.             if(currMagicSel < scriptRef.magicAmount ){
    160.                 magicStat.dmg = scriptRef.damage[currMagicSel];
    161.                 magicStat.dura = scriptRef.durability[currMagicSel];
    162.                 magicStat.effect = scriptRef.Seffect[currMagicSel];
    163.                 magicStat.stat = scriptRef.Sstat[currMagicSel];
    164.                 model = scriptRef.ModelsPrefix[currMagicSel];
    165.                 currMagicSel = currMagicSel + 1;
    166.             }
    167.             else{
    168.                 currMagicSel = 0;
    169.             }
    170.         }
    171.     }
    172. }
    173.  
    174. function Start(){
    175. }
    176.  
    177. function AttackCombo(){
    178.     if(!attackCombo[c]){
    179.         print("Please assign attack animation in Attack Combo");
    180.         return;
    181.     }
    182.     str = GetComponent(Status).addAtk;
    183.     matk = GetComponent(Status).addMatk;
    184.     var bulletShootout : Transform;
    185.     isCasting = true;
    186.     GetComponent(CharacterMotor).canControl = false;
    187.     MeleeDash();
    188.     while(conCombo > 0){
    189.         if(c >= 1){
    190.             mainModel.animation.PlayQueued(attackCombo[c].name, QueueMode.PlayNow).speed = attackAnimationSpeed;;
    191.         }else{
    192.             mainModel.animation.PlayQueued(attackCombo[c].name, QueueMode.PlayNow).speed = attackAnimationSpeed;;
    193.         }
    194.    
    195.     var wait : float = mainModel.animation[attackCombo[c].name].length;
    196.    
    197.     yield WaitForSeconds(atkDelay1);
    198.     c++;
    199.    
    200.     nextFire = Time.time + attackSpeed;
    201.             bulletShootout = Instantiate(attackPrefab, attackPoint.transform.position , attackPoint.transform.rotation);
    202.             bulletShootout.GetComponent(BulletStatus).Setting(str , matk , "Player");
    203.             conCombo -= 1;
    204.            
    205.     if(c >= attackCombo.Length){
    206.         c = 0;
    207.         atkDelay = true;
    208.         yield WaitForSeconds(wait);
    209.         atkDelay = false;
    210.     }
    211.    
    212.     }
    213.    
    214.     yield WaitForSeconds(attackSpeed);
    215.     isCasting = false;
    216.     GetComponent(CharacterMotor).canControl = true;
    217. }
    218.  
    219. function MeleeDash(){
    220.     meleefwd = true;
    221.     yield WaitForSeconds(0.2);
    222.     meleefwd = false;
    223.  
    224. }
    225.  
    226. //---------------------
    227. //-------
    228. function MagicSkill(skillID : int){
    229.     if(!skillAnimation[skillID]){
    230.         print("Please assign skill animation in Skill Animation");
    231.         return;
    232.     }
    233.     str = GetComponent(Status).addAtk;
    234.     matk = GetComponent(Status).addMatk;
    235.    
    236.     if(GetComponent(Status).mana < manaCost[skillID]){
    237.         return;
    238.     }
    239.     isCasting = true;
    240.     GetComponent(CharacterMotor).canControl = false;
    241.     mainModel.animation.Play(skillAnimation[skillID].name);
    242.        
    243.     nextFire = Time.time + skillDelay;
    244.     Maincam.GetComponent(ARPGcamera).lockOn = true;
    245.     var bulletShootout : Transform;
    246.    
    247.     var wait : float = mainModel.animation[skillAnimation[skillID].name].length -0.3;
    248.     yield WaitForSeconds(wait);
    249.     Maincam.GetComponent(ARPGcamera).lockOn = false;
    250. //    bulletShootout = Instantiate(skillPrefab[skillID], attackPoint.transform.position , attackPoint.transform.rotation);
    251.     bulletShootout.GetComponent(BulletStatus).Setting(str , matk , "Player");
    252.     yield WaitForSeconds(skillDelay);
    253.     isCasting = false;
    254.     GetComponent(CharacterMotor).canControl = true;
    255.     GetComponent(Status).mana -= manaCost[skillID];
    256. }
    257.  
    258. function Flinch(dir : Vector3){
    259.         knock = dir;
    260.         GetComponent(CharacterMotor).canControl = false;
    261.         KnockBack();
    262.         mainModel.animation.PlayQueued(hurt.name, QueueMode.PlayNow);
    263.         GetComponent(CharacterMotor).canControl = true;
    264.     }
    265.    
    266. function KnockBack(){
    267.     flinch = true;
    268.     yield WaitForSeconds(0.2);
    269.     flinch = false;
    270. }
    271.  
    272. @script RequireComponent (Status)
    273. @script RequireComponent (StatusWindow)
    274. @script RequireComponent (HealthBar)
    275. @script RequireComponent (PlayerAnimation)
    276. @script RequireComponent (FPSInputController)
    277. @script RequireComponent (CharacterMotor)
    278. @script RequireComponent (Inventory)
    EffectSettings:
    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using System.Linq;
    4. using UnityEngine;
    5.  
    6. public class EffectSettings : MonoBehaviour
    7. {
    8.   public float ColliderRadius = 0.2f;
    9.   public float EffectRadius = 0;
    10.   public GameObject Target;
    11.   public float MoveSpeed = 1;
    12.   public float MoveDistance = 20;
    13.   public bool IsHomingMove;
    14.   public bool IsVisible = true;
    15.   public bool DeactivateAfterCollision = true;
    16.   public float DeactivateTimeDelay = 4;
    17.   public LayerMask LayerMask = -1;
    18.  
    19.   public event EventHandler<CollisionInfo> CollisionEnter;
    20.   public event EventHandler EffectDeactivated;
    21.  
    22.   private GameObject[] active_key = new GameObject[100];
    23.   private float[] active_value = new float[100];
    24.   private GameObject[] inactive_Key = new GameObject[100];
    25.   private float[] inactive_value = new float[100];
    26.   private int lastActiveIndex;
    27.   private int lastInactiveIndex;
    28.   private int currentActiveGo;
    29.   private int currentInactiveGo;
    30.   private bool deactivatedIsWait;
    31.  
    32.     public void Awake(){
    33.         Target = GameObject.Find ("MagicRadius");
    34.     }
    35.  
    36.   public void OnCollisionHandler(CollisionInfo e)
    37.     {
    38.     for (int i = 0; i < lastActiveIndex; i++)
    39.     {
    40.       Invoke("SetGoActive", active_value[i]);
    41.     }
    42.     for (int i = 0; i < lastInactiveIndex; i++)
    43.     {
    44.       Invoke("SetGoInactive", inactive_value[i]);
    45.     }
    46.     var handler = CollisionEnter;
    47.     if (handler != null)
    48.       handler(this, e);
    49.     if (DeactivateAfterCollision && !deactivatedIsWait)
    50.     {
    51.       deactivatedIsWait = true;
    52.       Invoke("Deactivate", DeactivateTimeDelay);
    53.     }
    54.   }
    55.   public void OnEffectDeactivatedHandler()
    56.   {
    57.     var handler = EffectDeactivated;
    58.     if (handler != null)
    59.       handler(this, EventArgs.Empty);
    60.   }
    61.  
    62.   public void Deactivate()
    63.   {
    64.     OnEffectDeactivatedHandler();
    65.     gameObject.SetActive(false);
    66.   }
    67.  
    68.   private void SetGoActive()
    69.   {
    70.     active_key[currentActiveGo].SetActive(false);
    71.     ++currentActiveGo;
    72.     if (currentActiveGo >= lastActiveIndex) currentActiveGo = 0;
    73.   }
    74.  
    75.   private void SetGoInactive()
    76.   {
    77.     inactive_Key[currentInactiveGo].SetActive(true);
    78.     ++currentInactiveGo;
    79.     if (currentInactiveGo >= lastInactiveIndex) {
    80.       currentInactiveGo = 0;
    81.     }
    82.   }
    83.  
    84.   public void OnEnable()
    85.   {
    86.     for (int i = 0; i < lastActiveIndex; i++)
    87.     {
    88.       active_key[i].SetActive(true);
    89.     }
    90.     for (int i = 0; i < lastInactiveIndex; i++)
    91.     {
    92.       inactive_Key[i].SetActive(false);
    93.     }
    94.     deactivatedIsWait = false;
    95.   }
    96.  
    97.   public void OnDisable()
    98.   {
    99.     CancelInvoke("SetGoActive");
    100.     CancelInvoke("SetGoInactive");
    101.     CancelInvoke("Deactivate");
    102.     currentActiveGo = 0;
    103.     currentInactiveGo = 0;
    104.   }
    105.  
    106.   public void RegistreActiveElement(GameObject go, float time)
    107.   {
    108.     active_key[lastActiveIndex] = go;
    109.     active_value[lastActiveIndex] = time;
    110.     ++lastActiveIndex;
    111.   }
    112.  
    113.   public void RegistreInactiveElement(GameObject go, float time)
    114.   {
    115.     inactive_Key[lastInactiveIndex] = go;
    116.     inactive_value[lastInactiveIndex] = time;
    117.     ++lastInactiveIndex;
    118.   }
    119. }
    120.  
    121. public class CollisionInfo : EventArgs
    122. {
    123.   public RaycastHit Hit;
    124. }
     
  2. spil778

    spil778

    Joined:
    Mar 31, 2013
    Posts:
    57
    Nobody?
     
  3. VisualTech48

    VisualTech48

    Joined:
    Aug 23, 2013
    Posts:
    247
    For editing variables in a script you must remove the private. That way you can change any it to any value you want.
    example:
    Code (JavaScript):
    1. private var damage = 2.0; // By setting it to private, like the name states, the variable is private, and cannot be modified at any way.
    Code (JavaScript):
    1. public var damage = 2.0; // By setting it to public, like the name states, the variable is public, and can be modified at the inspector tab, however you do not need the public. But if you set it to private its private.
    Prefab+
    If you have a prefab, and drag it down towards the scene, you can change that values anyhow you wish. However if you change the script values in the prefab, almost 100% will the changes apply towards all the objects made by that prefab.

    If you want to have more different characters lets say "Strong-Mage" and "Weak-Mage" then you do the folowing. Make a new prefab. Drag the 1st prefab in the scene. Edit the way you want it, and drag the new prefab, with a new name, and its done.
     
  4. cakep

    cakep

    Joined:
    Oct 17, 2014
    Posts:
    7
    #pragma strict
    private var motor : CharacterMotor;
    private var moveDir : float = 0.0;
    var joyStick : GameObject;
    var walkingSound : AudioClip;

    // Use this for initialization
    function Awake () {
    motor = GetComponent(CharacterMotor);
    if(!joyStick){
    joyStick = GameObject.FindWithTag("JoyStick");
    }
    }

    // Update is called once per frame
    function Update () {
    var stat : Status = GetComponent(Status);
    if(stat.freeze || stat.flinch){
    motor.inputMoveDirection = Vector3(0,0,0);
    return;
    }
    if(Input.GetButton("Horizontal") || Input.GetButton("Vertical")){
    var moveHorizontal : float = Input.GetAxis("Horizontal");
    var moveVertical : float = Input.GetAxis("Vertical");
    }else if(joyStick){
    moveHorizontal = joyStick.GetComponent(MobileJoyStick).position.x;
    moveVertical = joyStick.GetComponent(MobileJoyStick).position.y;
    }

    // Get the input vector from kayboard or analog stick
    var directionVector : Vector3 = new Vector3(moveHorizontal, 0, moveVertical);
    //Vector3 directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));

    if (directionVector != Vector3.zero) {
    // Get the length of the directon vector and then normalize it
    // Dividing by the length is cheaper than normalizing when we already have the length anyway
    var directionLength : float = directionVector.magnitude;
    directionVector = directionVector / directionLength;

    // Make sure the length is no bigger than 1
    directionLength = Mathf.Min(1, directionLength);

    // Make the input vector more sensitive towards the extremes and less sensitive in the middle
    // This makes it easier to control slow speeds when using analog sticks
    directionLength = directionLength * directionLength;

    // Multiply the normalized direction vector by the modified length
    directionVector = directionVector * directionLength;
    }

    if(moveHorizontal != 0 || moveVertical != 0)
    transform.eulerAngles = new Vector3(transform.eulerAngles.x, Mathf.Atan2(moveHorizontal , moveVertical) * Mathf.Rad2Deg, transform.eulerAngles.z);
    //-----------------------------------------------------------------------------
    if(moveVertical != 0 && walkingSound && !audio.isPlaying|| moveHorizontal != 0 && walkingSound && !audio.isPlaying){
    audio.clip = walkingSound;
    audio.Play();
    }

    motor.inputMoveDirection = new Vector3(moveHorizontal , 0, moveVertical);
    motor.inputJump = Input.GetButton("Jump");
    }

    // Require a character controller to be attached to the same game object
    @script RequireComponent (CharacterMotor)

    do you know convert to Csharp hlp me plase