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 boolius · Apr 10, 2018 at 03:40 PM · scripting problemcomponentinterfaceabstractchild-to-gameobject

Change the state of a GameObjects Child using an Abstract Class with an Interface

So I wanna make a farm style game which uses generic tiles that can be modified to being the different ground types like grass/hedges/soil/... In order to give them the ability to grow and later on other automated timer related stuff, I made an abstract class Tile which then is defined by several options using the IAutomatic interface. Now to make it grow my plan was to simply enable the current state of the plant in the game object. When I tried it directly in the Grass script everything worked fine but now after putting it inside the grow class, I get a "NullReferenceException" leading to "transform.Find(name+state).gameObject.SetActive(true);".

Do you have any idea/solution how to solve this?

And a big thank you in advance :)

Grass class:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Grass : Tile
 {
     private int state = 1;
     public float minGrowTime = 5f;
     public float maxGrowTime = 20f;
     public string grassName = "Grass_State_";
     private float timer;
     Grass grass;
 
 
     void Start()
     {
         timer = Mathf.Lerp(minGrowTime, maxGrowTime, Random.value);
         grass = new Grass();
         grass.setAutomation(new Grow());
         timer = 1f;
     }
 
     void Update()
     {
         if (timer < 0)
         {
             grass.DoAutomation(state, grassName);
             timer = Mathf.Lerp(minGrowTime, maxGrowTime, Random.value);
         }
         else
         {
             timer -= Time.deltaTime;
         }
 
 
     }
 }

Abstract class:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public abstract class Tile : MonoBehaviour
 {
     IAutomatic automate;
 
     public void setAutomation(IAutomatic automate)
     {
         this.automate = automate;
     }
 
     public void DoAutomation(int State, string Name)
     {
         automate.automatic(State, Name);
     }
 }

Interface:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public interface IAutomatic
 {
     void automatic(int state, string name);
 }
 
 public class Grow : MonoBehaviour, IAutomatic
 {
     public void automatic(int state, string name)
     {
         print(name+state);
         transform.Find(name+state).gameObject.SetActive(true); //returns NullReferenceException
     }
 }

Screenshot of the Hierarchy:

alt text

screenshot-3.png (44.6 kB)
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Cornelis-de-Jager · Apr 10, 2018 at 04:09 PM

Short Answer:

 // This Creates a grass object that is not attached to a Transform
 Grass grass;
 ...
 grass = new Grass ();

Meaning that any Unity/Mono related functions do not work. You cannot use transform, gameObject or any similar once used to interact with the game world.

Long Answer:

Your problem is you are misusing Unity classes and C# classes. They cannot be used interchangeably. I am talking about your Grow Class.

How do you know your class is a Unity class as appose to a C# class (Ok I know that one is derived from the other, this is just to enforce that there is a difference in usage), well that's easy:

 /* Plain C# Class */
 public class NormalClass { 
    // This is a normal class, it works how any class would in C#
    // Doesn't interact with game wor;d
   // Create the class with ' Class c = new Class () '
 }
 
 /* Unity Class */
 public class NotNormalClass : MonoBehaviour { // <----- Mono is added 
    // Can Interact with game world, transform.move, GetComponent, tag etc..
   // CANNOT BE CREATED WITH Class c = new Class !!!
   // Created by .AddCompnent<Class> ()
   // Must be attached to game gameobject
 }

And your issue lies with your Grass class since 1. Its not a Class on its own so you cannot add it to your transform. So when you call transform.Find -- It can't find a transform since it is not attached to one.

 // Here you only make the class, you never assigned it to a GameObject, you only reference it
 
 Grass grass = new Grass();

You need to create a new Grass Class not part of the interface and attach it to the object. Then you can reference it with:

 Grass grass = GetComponent<Grass> ();


Comment
Add comment · Share
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

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

194 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 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 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 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 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 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 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

Can I Reference Object Components from another Scene? 0 Answers

Physics.Linecast does not return collider between start and end. 1 Answer

How can I reference a script in a specific gameobject if that same script exists in other gameobjects? 1 Answer

Child class cannot access property in method? 1 Answer

Tooltip doesn't work 0 Answers


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