Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 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 /
avatar image
1
Question by ActiveMars91 · Jan 11, 2021 at 08:04 PM · scripting probleminstancestatic-function

Having trouble calling a method between scripts without making the method static.

In a script attached to my main camera I've got a ray casting forward to any object I put under a crosshair. If the object that's being hit by the ray is a turret and the Q key is pressed then I want the turret to be upgraded. To do this I'm trying to run a method in the turret script from the camera script that will upgrade the turret. My problem here is that I'm getting an error when I call the method in the turret script:

An object reference is required for the non-static field, method, or property 'Turret.UpgradeTurret()'

Having read through some information, I've found that making the upgrade turret function static will fix the problem, but it will also mean that all the turrets in the scene will be upgraded at the same time. Sorry if the question is a little simple, I'm just starting up with learning c#.

This is from the script calling the UpgradeTurret method.

 if (targetTag == "Turret")
             {
                 ChangeTurretMaterial(selectedGameObject, raySelected);
                 Turret turret = selectedGameObject.GetComponent<Turret>();
                 Turret.UpgradeTurret();
             }


This is the methos being called.

 public void UpgradeTurret()
     {
           if (Input.GetKeyDown(KeyCode.Q))
         {
             Debug.Log("Upgrading Turret to level 2! You're bossing it!");
         }
     }

I've used this successfully elsewhere in the script and can't see why this isn't working. Thanks in advance, I'll add the full scripts below should that be of any help.

Camera script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraController : MonoBehaviour
 {
     private Transform _target;
     public GameObject cam;
     public Material raySelected;
     public Material defaultMaterial;
     public float rayDistance;
     // Start is called before the first frame update
     void Start()
     {
         
     }
 
     // Update is called once per frame
     void Update()
     {
         if (_target != null && _target.tag == "Tower Base")
         {
             var targetRenderer = _target.GetComponent<Renderer>();
             targetRenderer.material = defaultMaterial;
             _target = null;
         }
 
         if (_target != null && _target.tag == "Turret")
         {
             ChangeTurretMaterial(_target.gameObject, defaultMaterial);
             _target = null;
         }
 
         RaycastHit hit;
         if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, rayDistance))
         {
             var target = hit.transform;
             
             string targetTag = target.tag;
             GameObject selectedGameObject = target.gameObject;
             
             if (targetTag == "Tower Base")
             {
                 Renderer paintable = selectedGameObject.GetComponent<Renderer>();
                 Node node = target.GetComponent<Node>();
                 paintable.material = raySelected;
                 node.SpawnTurret(target.gameObject);
             }
             
             if (targetTag == "Turret")
             {
                 ChangeTurretMaterial(selectedGameObject, raySelected);
                 Turret turret = selectedGameObject.GetComponent<Turret>();
                 Turret.UpgradeTurret();
             }
             _target = target;
         }
     }
 
 
     public void ChangeTurretMaterial(GameObject _target, Material newMat)
     {
         GameObject selectedTurret = _target;
         Renderer[] children;
         
         children = _target.GetComponentsInChildren<Renderer>();
         foreach (Renderer rend in children)
         {
             
             var mats = new Material[rend.materials.Length];
             for (var j = 0; j < rend.materials.Length; j++)
             {
                 mats[j] = newMat;
             }
             rend.materials = mats;
         }
     }
 }
 



Turret script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Turret : MonoBehaviour
 {
 
    
     [Header("Turret movement")]
     public Transform target;
     public float range = 11.5f;
     public string enemyTag = "Enemy";
     public bool targeting = false;
     public Transform GunPivot;
     public float rotationSpeed = 10f;
 
     [Header("Turret firing")]
     public float fireRate = 1f;
     public float fireCountdown = 0f;
     public GameObject bullet;
     public Transform firingPoint;
     public int damage = 1;
 
 
     // Start is called before the first frame update
     void Start()
     {
         InvokeRepeating("UpdateTarget", 0f, 0.5f);
     }
 
     void UpdateTarget()
     {
         float shortestDistance = Mathf.Infinity;empty.
         GameObject nearestEnemy = null;
         GameObject[] enemies = GameObject.FindGameObjectsWithTag(enemyTag);
 
         foreach (GameObject enemy in enemies)
         {
             float distanceToEnemy = Vector3.Distance(transform.position, enemy.transform.position);
             if (distanceToEnemy < shortestDistance)
             {
                 shortestDistance = distanceToEnemy;
                 nearestEnemy = enemy;
                 
             }
         }
         if (nearestEnemy != null && shortestDistance <= range)
         {
             target = nearestEnemy.transform;
             targeting = true;
         }
 
         else
         {
             target = null;
         }
     }
 
     void Update()
     {
         if (target == null)
         {
             targeting = false;
             return;
         }
         Vector3 dir = target.position - transform.position;
         Quaternion lookRotation = Quaternion.LookRotation(dir);to euler angles,...
         Vector3 rotation = Quaternion.Lerp (GunPivot.rotation, lookRotation, Time.deltaTime * rotationSpeed).eulerAngles;
         GunPivot.rotation = Quaternion.Euler(rotation.x, rotation.y, 0f);
 
         fireCountdown -= Time.deltaTime;
         if (fireCountdown <= 0)
         {
             Shoot();
             fireCountdown = 1 / fireRate;
         }
         
     }
     
     void Shoot()
     {
         GameObject bulletGO = (GameObject) Instantiate(bullet, firingPoint.position, firingPoint.rotation);
         Bullet boomBall = bulletGO.GetComponent<Bullet>();
 
         if (boomBall != null)
         {
             boomBall.seek(target);
         }
     }
 
     public void UpgradeTurret()
     {
          
         if (Input.GetKeyDown(KeyCode.Q))
         {
             
             Debug.Log("Upgrading Turret to level 2! You're bossing it!");
 
         }
     }
 at which the turret can shoot.
     private void OnDrawGizmosSelected()
     {
         Gizmos.color = Color.red;
         Gizmos.DrawWireSphere(transform.position, range);
     }
 }
 



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
1
Best Answer

Answer by devondyer · Jan 11, 2021 at 08:27 PM

You are not referencing the "turret" game object when calling the upgrade function. Turret.UpgradeTurret(); should be turret.UpgradeTurret();. You made a reference to the object prior, but tried to call the upgrade function for the entire "Turret" class.

Comment
Add comment · Show 1 · 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
avatar image ActiveMars91 · Jan 11, 2021 at 08:52 PM 1
Share

I think I may have a breakdown. 6 hours I've been staring at this script trying to work it out. Thank you!

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

208 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 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 to obtain this prefab name in this circunstances 1 Answer

Variable Type for an Instance of any Script 2 Answers

Activating powerup on button click 0 Answers

How to change material property of an instance through script 0 Answers

Script not being executed 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