微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我想在Unity中从一个脚本访问另一个脚本的公共方法我已经尝试了以下代码

如何解决我想在Unity中从一个脚本访问另一个脚本的公共方法我已经尝试了以下代码

ObjectPoolingManager.Instance.GetBullet();中的公共Player.cs访问GameObject GetBulltet ()中的ObjectPoolingManager.cs中的Assets\SPF_Assets\Scripts\Game\Player.cs(4,7): error CS0138: A 'using namespace' directive can only be applied to namespaces; 'ObjectPoolingManager' is a type not a namespace. Consider a 'using static' directive instead 时出现以下错误

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ObjectPoolingManager; //Tried this,didnt work

public class Player : MonoBehavIoUr
{
    public Camera playerCamera;
    public GameObject bulletPrefab;
    // Update is called once per frame
    void Update() {
        if(Input.GetMouseButtonDown(0)){
            ObjectPoolingManager.Instance.GetBullet();

            GameObject bulletobject = Instantiate (bulletPrefab);
            bulletobject.transform.position = playerCamera.transform.position + playerCamera.transform.forward;
            bulletobject.transform.forward = playerCamera.transform.forward;
        }
    }
}

Player.cs脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ObjectPoolingManager : MonoBehavIoUr {

    private static ObjectPoolingManager instance;
    public static ObjectPoolingManager Instance { get { return instance; } }

    // Start is called before the first frame update
    void Awake () {
        instance = this;
    }

    public GameObject GetBulltet () {
        Debug.Log ("Hell0");
        return null;
    }
}

ObjectPoolingManager.cs

    <?PHP
require_once 'config.PHP';
require 'fpdf181/fpdf.PHP';

$query=MysqLi_query($conn,"select * from images where Id = '".isset($_POST['pdfId'])."'");
$gpdf=MysqLi_fetch_array($query);

$id = $gpdf['Id'];              //"ID";
$txt = $gpdf['CompanyN'];       //"Kamaya";
$txt2 = $gpdf['ContactP'];      //"Sales";
$txt3 = $gpdf['Email'];         //"sss@kamaya.com.my";
$chk = $gpdf['checkBox'];       //"KJ";
$chk1 = $gpdf['Others'];        //"Others";
$txt5 = $gpdf['Othersinput'];   //"Others";
$txt6 = $gpdf['Salesstaff'];    //salesstaff
$txt7 = $gpdf['LotNo'];         //"886515154M";
$txt8 = $gpdf['CusPartNo'];     //"cus123456";
$txt9 = $gpdf['Title'];         //"cannot work";
$chk2 = $gpdf['checkBox2'];
$chk9 = $gpdf['process'];
$txt10 = $gpdf['inputBox2'];    //"JIG STACKING";
$chk10 = $gpdf['Others2'];
$txt24 = $gpdf['Othersinput2'];
$txt11 = $gpdf['Rate'];         //"10%";
$txt12 = $gpdf['FailDate'];     //"2020-09-06"; 
$chk3 = $gpdf['checkBox3'];     //"return";
$txt14 = $gpdf['ETA'];          //"2020-09-06";
$txt15 = $gpdf['Quantity'];     //"5";
$chk4 = $gpdf['checkBox4'];     //"No required";
$txt17 = $gpdf['ETD'];          //"2020-09-06";
$txt18 = $gpdf['Quantity2'];    //"5";
$chk5 = $gpdf['checkBox5'];     //"required";
$chk6 = $gpdf['Method'];        //"Phone";
$txt19 = $gpdf['VisitBy'];      //"me";
$txt20 = $gpdf['visitDate'];    //"2020-09-06";
$txt21 = $gpdf['Reportdate'];   //"2020-09-06";
$txt22 = $gpdf['Textarea'];     //"qqwweerrttyyuuiiooppllkkjjhhggffddssaazxxzxccvvbnm.,lpiujhhhgvasgvybagfvabfvgyabuibaubfvuigaubvuailbuaubkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk";



$pdf = new FPDF();
$pdf->AddPage('P','A4',0);
    
    $pdf->SetFont('Arial','B',9);
    //$pdf->SetXY(7,5);
    $pdf->Multicell(120,5,"KAMAYA ELECTRIC (M) SDN.BHD\nCUSTOMER COMPLAINT SHEET",1,"C");
    
    $pdf->SetXY(130,10);
    $pdf->Multicell(69,"Date Issued :".date('yy-m-d'),15);
    $pdf->Multicell(69,"Report No :".$id,"C");
    
    $pdf->Multicell(26,7.5,"Complaint\nInitiator","C");
    
    $pdf->SetXY(36,20);
    $pdf->Multicell(163,"Co.Name :".$txt,1);
    
    $pdf->SetXY(36,25);
    $pdf->Multicell(163,"Contact Person: ".$txt2,30);
    $pdf->Multicell(163,"Email / Contact No: ".$txt3,1);
    
    $pdf->Multicell(26,"Sales\nOffice",35);
    $pdf->Multicell(163,$chk,40);
    $pdf->Multicell(163,$chk1." ".$txt5,45);
    $pdf->Multicell(163,"Sales Staff : ".$txt6,10,"Product",50);
    $pdf->Multicell(163,"Lot No : ".$txt7,55);
    $pdf->Multicell(163,"Customer Part No : ".$txt8,15,"Failure",60);
    $pdf->Multicell(163,"Title : ".$txt9,65);
    $pdf->Multicell(163,"Scene : ".$chk2." \t ",65);
    $pdf->Write(5,$chk9. "" . $txt10);
    
    $pdf->SetXY(36,$chk10. "" . $txt24);
    
    $pdf->SetXY(36,70);
    $pdf->Multicell(163,"Rate : ".$txt11." \t "."Date :".$txt12,"Failure\nSample",75);
    $pdf->Multicell(163,8,$chk3." \t "."ETA :".$txt14,83);
    $pdf->Multicell(163,7,"Quantity :".$txt15,"Replacement",90);
    $pdf->Multicell(163,$chk4." \t "."ETD :".$txt17,98);
    $pdf->Multicell(163,"Quantity :".$txt18,"Reply",105);
    $pdf->Multicell(163,$chk5."\t Method : ".$chk6,1);
    $pdf->SetXY(36,110);
    $pdf->Multicell(163,"Visit By : ".$txt19."\t Date : ".$txt20,115);
    $pdf->Multicell(163,"Report required Date : ".$txt21,"Failure Details\n(Attached photo if available)",120);
    $pdf->Multicell(163,$txt22,1);
    
//display image and document
$temp = "37";
$temp2 = "154";
$temp3 = "51";
$temp4 = "185";
$imgrow=0;
$allFiles1 = scandir("F:/xampp/htdocs/new3/uploads/KM2009001");
$files1 = array_diff($allFiles1,array('.','..')); // To remove . and .. 
foreach($files1 as $file1)
{
    $tt = "F:/xampp/htdocs/new3/uploads/KM2009001"."/".$file1;
    
    if(is_file($tt)){
        $allowed = array('gif','png','jpg');
        $ext = pathinfo($tt,PATHINFO_EXTENSION);
        if (in_array($ext,$allowed)) {
            $imgrow ++;
            $pdf->SetXY(37,154);
            if($imgrow === 1)
            {
                $pdf->Image($tt,$temp,$temp2,30,20);
                $temp+=40;
            }
            $imgrow=0;
        }
        else
        {
            $pdf->SetXY($temp3,$temp4);
            $file = basename($tt,$file1);
            $pdf->displayPreferences('displayDocTitle');
            $pdf->Write(5,$file);
            $temp4 += 5;
            
        }
        
    }
}
$pdf->Line(36,180,199,180);
$pdf->Line(199,120);

$pdf->SetXY(10,180);
$pdf->Multicell(26,"Attachments\n(if available)","C");
$pdf->SetXY(36,180);
$pdf->Multicell(15," NO ",185);
$pdf->Multicell(15," 1 ",190);
$pdf->Multicell(15," 2 ",195);
$pdf->Multicell(15," 3 ",200);
$pdf->Multicell(15," 4 ",205);
$pdf->Multicell(15," 5 ","C");
$pdf->SetXY(51,180);
$pdf->Multicell(148,"Attachments",185);
$pdf->Multicell(148,"",190);
$pdf->Multicell(148,195);
$pdf->Multicell(148,200);
$pdf->Multicell(148,205);
$pdf->Multicell(148,"C");
 
$pdf->Output();
?>

解决方法

我多次阅读了代码,终于意识到您的答案已经对您的问题是正确的:

I am getting the below error while accessing
ObjectPoolingManager.Instance.GetBullet(); in Player.cs
from public GameObject GetBulltet () which is in ObjectPoolingManager.cs

我在您的代码中对此进行了验证,即使没有这样的函数,您实际上也调用了函数ObjectPoolingManager.Instance.GetBullet(),而实际上应该是GetBulltet()的函数GetBullet()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。