Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Ipad0214 · Sep 01, 2015 at 08:32 AM · c#trigger

C# access a generic list from another controller

Hello,

i am very new to unity and i cant solve this Problem:

 public class IodeController : MonoBehaviour {
 
     private PlayerController player;
 
     public ResourceModel IronModel;
     
     public void OnTriggerEnter(Collider other)
     {
         Gather(other);
     }
 
     public void OnTriggerStay(Collider other)
     {
         Gather(other);
     }
 
     public void OnTriggerExit(Collider other)
     {
         Debug.Log("Left Trigger");
     }
 
     public void Gather(Collider other)
     {
         player = other.GetComponent<PlayerController>();
 
         if (Input.GetButton("Interact"))
         {
             Debug.Log("i am In");
             var ironOre = new ResourceModel("Iron Ore", "Ore");
             Debug.Log(ironOre.Name);
             Debug.Log(player.Inventory.Count);
         }
     }
 }

this is the other class:

 [RequireComponent(typeof(CharacterController))]
 public class PlayerController : MonoBehaviour {
 
     public float moveSpeed;
     public float rotationspeed;
     CharacterController cc;
     AnimationCurve ac;
 
     private Animation aa;
 
     public List<ResourceModel> Inventory;
 
     // Use this for initialization
     void Start ()
     {
         cc = GetComponent<CharacterController>();
         aa = this.GetComponent<Animation>();
         Inventory = new List<ResourceModel>();
 
         Debug.Log(Inventory);
     }
     
     // Update is called once per frame
     void Update ()
     {
         transform.Rotate(new Vector3(0, Input.GetAxis("Horizontal") * rotationspeed * Time.deltaTime, 0));
         Vector3 forward = new Vector3();
 
         if (Input.GetButton("Vertical") && Input.GetButton("Sprint"))
         {
             aa.Play("run");
             forward = Input.GetAxis("Vertical") * transform.TransformDirection(Vector3.forward) * (moveSpeed * 4);
         }
         else if (Input.GetButton("Vertical"))
         {
             aa.Play("walk");
             forward = Input.GetAxis("Vertical") * transform.TransformDirection(Vector3.forward) * moveSpeed;
         }
         else
         {
             aa.Play("idle");
         }    
 
         cc.Move(forward * Time.deltaTime);
         cc.SimpleMove(Physics.gravity);
     }
 
     public void AddItem(ResourceModel model)
     {
         this.Inventory.Add(model);
         this.Inventory.ForEach(x => Debug.Log(x.Name));
     }
 
     public ResourceModel GetItem(string itemName)
     {
         return this.Inventory.SingleOrDefault(x => x.Name.Equals(itemName));
     }
 }

as you can see i want to set a new Instance of ResourceModel to the Inventoy, which i created on the Start function on the PlayerController. The INstance is there as aspected. But if i try to access it on the GatherOre Function the List is null.

Can somebody explain that? to me?

Comment
Add comment · Show 11
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mikilo · Sep 01, 2015 at 09:02 AM 0
Share

Hello!

Are you saying that this line writes 0 or it throw a null exception?

          Debug.Log(player.Inventory.Count);
avatar image Ipad0214 · Sep 01, 2015 at 09:10 AM 0
Share

Hi,

it´s a null Exception. A 0 would better, but sadly its a null reference.

avatar image Mikilo · Sep 01, 2015 at 09:36 AM 0
Share

The truth is, a null exception is better. Because the error is obvious while 0 is weird.

If it is a null exception, it would mean 2 things:

Whether player or player.Inventory is null.

If player is null, it says that:

player = other.GetComponent();

The Collider other does not have PlayerController in it. Check the GameObject of the Collider.

If player.Inventory is null, it is worst.

avatar image Ipad0214 · Sep 01, 2015 at 09:47 AM 0
Share

I think i got an idea about the Problem.

The "other" is my Character, which only have a CharacterController and no collider. So the Collider "other" is null. Do you have an advice to solve that?

avatar image Ipad0214 · Sep 03, 2015 at 06:12 AM 1
Share

Hi,

i found a solution:

$$anonymous$$y CharacterController is working as a collider as aspected. and the $$anonymous$$eshCollider doesn´t work somehow so i added a Box Collider on it without "trigger" and now it works perfectly. THanks for your help!

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I collect collider2Ds(Trigger) the player is in into an array 1 Answer

Weird text field bug? 1 Answer

Object reference not set to an instance of an object 0 Answers

Enabling particle system with trigger 1 Answer

Stop AudioSource on Trigger!!! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges