Search Unity

Please help me when answers not working

Discussion in 'Scripting' started by honzapat, Nov 4, 2017.

  1. honzapat

    honzapat

    Joined:
    Oct 3, 2015
    Posts:
    18
    Becuase if answers not working because of google captcha i posting it here
    Hi iam making and inventory pickup system
    my inventory script holding ammo and of player has the gun
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class WeaponScr : MonoBehaviour {
    5.  
    6. // Use this for initialization
    7. public int ammo;
    8. public bool isOwn;
    9.  
    10. }
    and pickup script that toggle the isOwn to true and another to add 40 rounds to ammo
    but it isnt working
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Pickup : MonoBehaviour {
    6. public playerScript WeaponsScr;
    7. public GameObject WeaponItem;
    8.  
    9. void Update()
    10. {
    11. // Rotate the object around its local X axis at 1 degree per second
    12. transform.Rotate(0,50*Time.deltaTime,0);
    13. }
    14. void OnTriggerEnter(Collider other) {
    15. WeaponsScr playerScript = WeaponItem.GetComponent("WeaponScr");
    16. playerScript.isOwn = true;
    17. Destroy(gameObject);
    18. }
    19.  
    20. }
    can someone fix it and somehow explain me how to add 40 to the ammo varible?
     
  2. P_Mason

    P_Mason

    Joined:
    Apr 6, 2014
    Posts:
    14
    Line 15 change "WeaponsScr" to "WeaponScr". You've just added an extra "s" by mistake.

    then before destroy gameobj add

    playerScript.ammo = playerScript.ammo + 40;
     
  3. honzapat

    honzapat

    Joined:
    Oct 3, 2015
    Posts:
    18
    Still error CSO246 The ty or namespace name "playerScript" could not be found.Are you mussing any assembly reference?
     
  4. honzapat

    honzapat

    Joined:
    Oct 3, 2015
    Posts:
    18
    On line 6 word 13
     
  5. P_Mason

    P_Mason

    Joined:
    Apr 6, 2014
    Posts:
    14
    There aren't 13 words on line 6. Swap WeaponScr (and remove the extra s) and playerScript around on line 6
     
  6. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    It appears you don’t have a class called playerScript.
     
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Since you're creating a local variable inside OnTriggerEnter, you can simply erase line 6 :)
     
  8. honzapat

    honzapat

    Joined:
    Oct 3, 2015
    Posts:
    18
    removed line 6 and now this
    Assets/scripts/Pickup.cs(15,45): error CS0266: Cannot implicitly convert type `UnityEngine.Component' to `WeaponScr'. An explicit conversion exists (are you missing a cast?)
     
  9. honzapat

    honzapat

    Joined:
    Oct 3, 2015
    Posts:
    18
    Figured the problem on unity help discord server
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Glad you got it resolved. :)