Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 xyHeat · Mar 26, 2016 at 10:40 AM · scripting problemscripting beginnerturrettower-defensebased

Inherance script problem [C#]

Hi everybody ! I have a problem with base script : i want to use it for differents types of turrets but i don't know how to solve problems : Instantiate, transform, startCoroutine and StopCoroutine are not recognized...

This is my two scripts :

BaseTower :

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class BaseTower
 {
     private string towerName;
     private string towerDescription;
     private int towerPrice;
     private float towerDamage;
     private int towerRange;
 
 
     public string TowerName
     {
         get { return towerName; }
         set { towerName = value; }
     }
 
     public string TowerDescription
     {
         get { return towerDescription; }
         set { towerDescription = value; }
     }
 
     public int TowerPrice
     {
         get { return towerPrice; }
         set { towerPrice = value; }
     }
 
     public float TowerDamage
     {
         get { return towerDamage; }
         set { towerDamage = value; }
     }
 
     public int TowerRange
     {
         get { return towerRange; }
         set { towerRange = value; }
     }
 
 
     private GameObject mob;
     public GameObject bullet;
     public GameObject gun;
     public int power = 100;
 
     private bool isFiring;
     private IEnumerator fireRoutine;
 
     void Update()
     {
         if (mob != null)
         {
             transform.LookAt(mob.transform.position);
         }
     }
 
 
     void OnTriggerEnter(Collider col)
     {
         if (col.tag == "MobCol")
         {
             mob = col.gameObject;
             fireRoutine = FireRoutine();
             StartCoroutine(fireRoutine);
         }
     }
 
     void OnTriggerExit(Collider col)
     {
         // Make sure our initial mob should be removed
         if (col.gameObject == mob)
         {
             if (fireRoutine != null)
             {
                 StopCoroutine(fireRoutine);
             }
 
             mob = null;
         }
     }
 
     public IEnumerator FireRoutine()
     {
         // Use a Coroutine for this
         WaitForSeconds wait = new WaitForSeconds(0.2f);
 
         // Keep ourselves from firing at nothing
         while (mob != null)
         {
             Fire();
 
             // Return our yield instance
             yield return wait;
         }
     }
 
     public void Fire()
     {
         GameObject towerBullet = (GameObject)Instantiate(bullet, gun.transform.position, Quaternion.identity);
         towerBullet.GetComponent<Rigidbody>().AddForce(transform.forward * power);
     }
 }
 

and my tower script (not finished yet because i can't fix the first problem ) :

 using UnityEngine;
 using System.Collections;
 
 public class RifleTower : BaseTower {
 
     public RifleTower()
     {
         TowerName = "Rifle tower";
         TowerDescription = "Fast fire rate";
         TowerPrice = 100;
         TowerRange = 5;
         TowerDamage = 1f;
     }
 
 
 }
 

Thank you ! bye, xyHeat

Comment
Add comment · Show 3
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 Positive7 · Mar 26, 2016 at 12:47 PM 1
Share

class should derive from $$anonymous$$onoBehaviour for transform and coroutine to work (and lots of other things)

 public class BaseTower : $$anonymous$$onoBehaviour

or you can make an IBaseTower interface or abstract class for the fields

then your BaseTower class should look like public class BaseTower : $$anonymous$$onoBehaviour, IBaseTower

avatar image xyHeat Positive7 · Mar 26, 2016 at 01:28 PM 0
Share

If i do that, can i use my second script ? ( with : public class RifleTower : BaseTower )

avatar image Positive7 xyHeat · Mar 26, 2016 at 01:59 PM 0
Share

It should work fine with the first method fine. (Just add $$anonymous$$onoBehaviour)

Second one looks better I$$anonymous$$O.

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

42 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

Related Questions

How can i access another scripts GameObject Array to check its inside if there are any tower 1 Answer

How to use value from a function in another function 1 Answer

Creating a single class or use polymorphism? 1 Answer

GameObject.Find : why not working? 3 Answers

Fading in Smoothly between Sprites 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